Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15422 Accepted Submission(s): 6425

Problem Description

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl's fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls' lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet's cycle is 9 and its cyclic count is 2:

Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.

CC is satisfied with his ideas and ask you for help.

Input

The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.

Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by 'a' ~'z' characters. The length of the string Len: ( 3 <= Len <= 100000 ).

Output

For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.

Sample Input

3
aaa
abca
abcde

Sample Output

0
2
5

分析

  • 题意:将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数。
  • 必须找出最小循环节,才可以求出满足题意中的最小添加数。
  • KMP算法中,可以利用k = len - nxt[len]来求出最小循环节长度k。
    • KMP算法中匹配串b指针j的移动可以理解为长度为最小循环节的移动。
  • 然后只需要判断在最小循环节数大于1的情况下,串是否是由整数个最小循环子串构成。如果是,就不需要添加。
  • 如果需要添加,则只需要串长对最小循环节数取余求出多余长度,然后最小循环长度减去这个多余长度,即可求出需要添加数目。
    string a;
int n;
int nxt[100001];
void getnxt()
{
nxt[0] = -1;
int j = 0,k = -1;
while(j<n)
{
if(k==-1||a[j] == a[k])
nxt[++j] = ++k;
else
k = nxt[k];
}
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>a;
n = a.length();
getnxt();
int k = n-nxt[n];//k为最小循环节长度
if(n%k==0&&k!=n)cout<<0<<endl;//不需要添加
else
cout<<k-nxt[n]%k<<endl;
}
return 0;
}

HDU-3746-Cyclic Nacklace(KMP,循环节)的更多相关文章

  1. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  2. HDU 3746 Cyclic Nacklace(kmp next数组运用)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  3. HDU 3746 Cyclic Nacklace (KMP找循环节)

    题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...

  4. hdu 3746 Cyclic Nacklace KMP循环节

    Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...

  5. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  6. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

  7. HDU 3746 Cyclic Nacklace KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...

  8. HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

    Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...

  10. HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解

    思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...

随机推荐

  1. Android HandlerThread源码解析

    在上一章Handler源码解析文章中,我们知道App的主线程通过Handler机制完成了一个线程的消息循环.那么我们自己也可以新建一个线程,在线程里面创建一个Looper,完成消息循环,可以做一些定时 ...

  2. Codeforces 550B 【暴力】

    题意: 有n个数字, 要求在这n个数中选出至少两个数字, 使得它们的和在L,R之间,并且最大的与最小的差值要不小于x 思路: 撒比了啊... 根据状态的话一共也就是2^15-直接暴力,二进制的小魅力还 ...

  3. 分布式集群环境下,如何实现session共享三(环境搭建)

    这是分布式集群环境下,如何实现session共享系列的第三篇.在上一篇:分布式集群环境下,如何实现session共享二(项目开发)中,准备好了一个通过原生态的servlet操作session的案例.本 ...

  4. Eclipse - Maven项目Update Project后jdk版本变成1.5

    问题与分析 最近遇到个奇怪的问题,在Eclipse里对一个Maven项目进行Update Project(快捷键是 Alt+F5),原本jdk为1.8的项目忽然就变成了1.5,于是就报了一些错误. 我 ...

  5. c++运费符优先级

    L:左      R:右

  6. 解决error while loading shared libraries

    ldd print shared library dependencies.可以查看哪些库没有找到. 这个进程启动失败,使用ldd命令可以发现是因为memcache库没有发现.把该so文件放入/lib ...

  7. Appium问题记录

    1.Appium 提示覆盖安装Appium Android Input Manager for Unicode 问题 安卓手机在新版本中Appium 总是提示覆盖安装Appium Android In ...

  8. Dima and Magic Guitar CodeForces - 366E

    Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425 ...

  9. 条形码问题 dp+求某个序列在某种排列中的序号的方法

    题目 条形码是一种由亮条(Light Bar)和暗条(Dark Bar)交替出现且以暗条为起头的符号,每条都占有若干个单位宽.图33-1给出了一个含有4个条的条形码,它延续了1+2+3+1=7单位的宽 ...

  10. B. Code For 1 一个类似于线段树的东西

    http://codeforces.com/contest/768/problem/B 我的做法是,观察到,只有是x % 2的情况下,才有可能出现0 其他的,都是1来的,所以开始的ans应该是R - ...