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. JDBC连接池以及动态SQL处理

    复习一下: 1.先创建一个properties配置文件 ClasssName=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@服务器IP:端 ...

  2. 【HDU1402】【FFT】A * B Problem Plus

    Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...

  3. mysql 刘道成视频教程 第3课

    第3课: 1.mysql中条件使用关键字where: 2.查 select name,content from msg ; select name,content 控制列 where id>2 ...

  4. 去掉Visual Studio 编辑器里中文注释的红色波浪线 转载

    我们通常用visual studio进行开发的时候,我们通常会用到一款比较流行比较方便的插件,那就是Visual Assist X,它可以增强Microsoft开发环境下的编辑能力,支持C/C++,C ...

  5. C++ 静态、动态链接库的简单实现

    一.什么是静态链接库,什么是动态链接库? 1.静态链接库就是你使用的.lib文件,库中的代码最后需要连接到你的可执行文件中去,所以静态连接的可执行文件一般比较大一些.在静态库情况下,函数和数据被编译进 ...

  6. oracle简单两个操作

    sqlplus sys/密码 as sysdba ALTER USER 账号 IDENTIFIED BY 新密码; select *  from (select rownum 别名 ,表名.* fro ...

  7. 配置并学习微信JS-SDK(1)

    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 微信JS-SDK ...

  8. 桂电在线-转变成bootstrap版

    由于angularjs的不熟悉,而且SEO需要学习更多东西,于是先采用bootstrap版本,毕竟工作上也需要使用bootstrap,然后参照视频教程学习. bootstrap 基本模板 <!D ...

  9. jQuery提供的小方法

    jQuery提供的小方法: 1.选择器 + 事件 + 函数 = 复杂的交互 2.循环处理与选择器匹配的各个元素:each() $("#").each(function(){     ...

  10. Smarty实现HTML静态化页面

    <?phprequire_once("./config/config.php"); ob_start();$id=$_GET[id];$sql="select * ...