Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2538    Accepted Submission(s): 1154
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

题意:给定一个串。求至少加入几个字符能使其内部至少反复两次的反复串。

题解:直接对所给串求next数组,len-next[len]即为循环节点。

#include <stdio.h>
#define maxn 100002 char str[maxn];
int len, next[maxn]; void getNext()
{
int i = 0, j = -1;
next[0] = -1;
while(str[i]){
if(j == -1 || str[i] == str[j]){
++i; ++j; next[i] = j;
}else j = next[j];
}
len = i; //save time
} int main()
{
int cas, ans;
scanf("%d", &cas);
while(cas--){
scanf("%s", str);
getNext();
ans = len - next[len];
if(ans != len && len % ans == 0)
ans = 0;
else ans = ans - next[len] % ans;
printf("%d\n", ans);
}
return 0;
}

HDU3746 Cyclic Nacklace 【KMP】的更多相关文章

  1. hdu3746 Cyclic Nacklace【nxt数组应用】【最小循环节】

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

  2. hdoj 3746 Cyclic Nacklace【KMP求在结尾加上多少个字符可以使字符串至少有两次循环】

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

  3. hdu-----(3746)Cyclic Nacklace(kmp)

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

  4. HDU_3746 Cyclic Nacklace 【KMP的应用】

    一.题目 HDU3746 二.分析 KMP比较好解决的一个问题:如果求一个串中的循环节? 仔细回想KMP的用法,重点是next数组,相当于就是后缀和前缀的比较,那么不正是方便了我们确定循环节? 如果以 ...

  5. HDU3746 Cyclic Nacklace —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    M ...

  6. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  7. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  8. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  9. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

随机推荐

  1. VC 透明滑动控件Slider Control

    操作系统:Windows 7软件环境:Visual C++ 2008 SP1本次目的:为滑动控件设置背景透明 经常在编写有背景的程序时,滑动控件Slider Control看起来与背景十分不合,我们可 ...

  2. Android 动态刷新listview中的数据

    之前一直困扰listview刷新后位置的问题,现在才明白,原来不能重新设置listview的adapter,而应该用notifyDataSetChanged()来刷新,这样位置就不会置顶. 下面做了一 ...

  3. 【HDOJ】1026 Ignatius and the Princess I

    这道题搞了很久啊.搜索非常好的一道题.昨天想了2小时,以为是深搜,但后来发现深搜怎么也没法输出正确路径.今天拿宽搜试了一下,问题就是普通的队列宽搜没法得到当前时间最小值.看了一下讨论区,发现优先级队列 ...

  4. tlplayer for ios V1.1.2加密测试版本(修复1.1.1版本 for ios7播放闪退问题)

    此版本主要修复了ios7播放列表导致的程序闪退问题,方便大家测试加密与非加密视频. 此为tlplayer for ios版本,可以播放加密视频与非加密视频. 加密视频下载地址:http://blog. ...

  5. XML PUBLISHER输出excel禁止自动将数字格式化处理

    方案1: 在 rtf template 里头添加   =T("域"). 方案2: 加个无中断空格  0xA0               In the rtf template, ...

  6. ArrayList Iterator remove java.lang.UnsupportedOperationException

    在使用Arrays.asList()后调用add,remove这些method时出现 java.lang.UnsupportedOperationException异常.这是由于Arrays.asLi ...

  7. 关于开源框架GPUImage 的简单说明

    GPUImage是一个非常棒的图像处理的开源库,里面提供了非常非常多的滤镜效果来加工图片. 不过就是因为太多效果了,而且对于程序员来说,那么多效果并不清楚知道要用那一个.于是我就使用提供的默认值,加上 ...

  8. (转)如何正确使用C++多重继承

    http://bigasp.com/archives/486 如何正确使用C++多重继承 2011年06月17日 — Asp J 原创文章,转载请注明:转载自Soul Apogee本文链接地址:如何正 ...

  9. MFC UpdateData自动更新

    嗯,添加一个按钮和我们自己定义的成员变量 关联. 方法1. 在*.rc 或者class 类视图中点击[add variable],就会自动生成DDX_Text(*,*)的内容及其IF 方法2.  自己 ...

  10. POJ 3254 (状态压缩DP)

    思路:状态压缩dp,用二进制位的1表示放了,0表示没有放.设dp[i][j],表示第i行状态为j时,前i行的方案数,状态转移方程就是 dp[i][j] += dp[i-1][k],j与k这两个状态不冲 ...