Cyclic Nacklace

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

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
#include<stdio.h>
#include<string.h>
#define MAX 100100
char p[MAX],s[MAX];
int f[MAX];
int len;
void getfail()
{
int i,j;
f[0]=f[1]=0;
for(i=1;i<len;i++)
{
j=f[i];
while(j&&s[i]!=s[j])
j=f[j];
f[i+1]=s[i]==s[j]?j+1:0;
}
}
void kmp()
{
int i,j;
int ans=0;
ans=len-f[len];
if(len%ans==0&&f[len])
printf("0\n");
else
printf("%d\n",ans-(f[len]%ans));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
len=strlen(s);
getfail();
kmp();
}
return 0;
}

  

hdoj 3746 Cyclic Nacklace【KMP求在结尾加上多少个字符可以使字符串至少有两次循环】的更多相关文章

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

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

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

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

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

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

  4. hdu 3746 Cyclic Nacklace KMP循环节

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

  5. HDU 3746 Cyclic Nacklace KMP

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

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

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

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

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

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

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

  9. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. 修改PYTHONPATH的一种方法(在Window平台和Ubuntu下都有效)

    1.显示PYTHONPATH 2.修改PYTHONPATH 通过PYTHONPATH 中的任何 .pth 文件来添加pythonpath.比如我想添加/home/aa这个路径到pythonpath里, ...

  2. iOS-assign、copy 、retain等关键字的含义

    iOS中assign.copy .retain等关键字的含义 assign: 简单赋值,不更改索引计数 copy: 建立一个索引计数为1的对象,然后释放旧对象 retain:释放旧的对象,将旧对象的值 ...

  3. Undefined symbols for architecture armv7: "_OBJC_METACLASS_$_ _OBJC_CLASS_$_ ld: symbol(s) not found for architecture armv7错误

    Undefined symbols for architecture armv7:  "_OBJC_METACLASS_$_MWPhotoBrowser", referenced ...

  4. jQuery HTML CSS 方法

    jQuery HTML / CSS 方法 下面的表格列出了所有用于处理 HTML 和 CSS 的 jQuery 方法. 下面的方法适用于 HTML 和 XML 文档.除了:html() 方法. 方法 ...

  5. CSS3弹性盒模型flex box快速入门 2016.03.16

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. 利用maven的resources、filter和profile实现不同环境使用不同配置文件

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  7. Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)

    一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...

  8. codevs 4511 信息传递(NOIP2015 day1 T2)

    4511 信息传递 NOIP2015 day1 T2 时间限制: 1 s 空间限制: 128000 KB 传送门 题目描述 Description 有个同学(编号为 1 到)正在玩一个信息传递的游戏. ...

  9. 翻纸牌 高校俱乐部 英雄会 csdn

    题目描述 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张纸牌(由正翻到反,或者 ...

  10. angularjs应用骨架

    使用典型的类库时,你可以选择并使用你所喜欢的功能:而对于angularjs框架来说,必须把它看成一个完整的套件来使用,框架中的所有的东西都包含在里面,接下来将会介绍angular的基础模块,这样你就可 ...