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. Android更新UI的两种方法——handler与runOnUiThread()

    在Android开发过程中,常需要更新界面的UI.而更新UI是要主线程来更新的,即UI线程更新.如果在主线线程之外的线程中直接更新页面 显示常会报错.抛出异常:android.view.ViewRoo ...

  2. 批量翻转PNG图片

    用了好几个软件都不好用. 要么不能翻转PNG, 要么翻转之后没有透明度了. 基本上全是图形界面, 要鼠标批量拖放. 所以, 还是自己动手, 写一个批量png翻转工具. #include <ios ...

  3. ASP.Net定时任务执行

    原料: System.Timers.Timer():通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序.消息没有特别的要求:缺点是不支持直接的拖放,需要手工编码. Timer的 ...

  4. [转]显示文件命令:cat、more、less、tail、touch详解

    cat命令cat命令连接文件并打印到标准输出设备上.cat经常用来显示文件的内容,类似于下的type命令. 一般格式:cat [选项] 文件说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取 ...

  5. JavaScript学习总结【5】、JS DOM

    1.DOM 简介 当页面加载时,浏览器会创建页面的文档对象模型(Document Object Model).文档对象模型定义访问和处理 HTML 文档的标准方法.DOM 将 HTML 文档呈现为带有 ...

  6. JavaScript学习心得(二)

    一选择DOCTYPE DOCTYPE是一种标准通用标记语言的文档类型声明,目的是告诉标准通用标记语言解析器使用什么样的文档类型定义(DTD)来解析文档. 网页从DOCTYPE开始,即<!DOCT ...

  7. php约瑟夫环

    <?php function popChar($str , $m , $current = 0){ $number = count($str); $num = 1; if(count($str) ...

  8. ART模式和Dalvik模式的异同

    Dalvik模式 如果要解释清楚什么是ART模式,我们就需要从Android系统的应用编译模式说起,我们都知道Android系统是以Linux系统为底层构建的,Android系统是开源(源代码公开)的 ...

  9. AppDomain与进程、线程、Assembly之间关系

    AppDomain是CLR的运行单元,它可以加载Assembly.创建对象以及执行程序 AppDomain是CLR实现代码隔离的基本机制.   每一个AppDomain可以单独运行.停止:每个AppD ...

  10. python【第十二篇下】操作MySQL数据库以及ORM之 sqlalchemy

    内容一览: 1.Python操作MySQL数据库 2.ORM sqlalchemy学习 1.Python操作MySQL数据库 2. ORM sqlachemy 2.1 ORM简介 对象关系映射(英语: ...