【KMP】Cyclic Nacklace
KMP算法
next[]深入了解,做到这题才真正明白next[]的用法,希望后面的题目能理解的更深刻。
Problem Description
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
Output
Sample Input
3
aaa
abca
abcde
Sample Output
0
2
5
Author
Source
#include<stdio.h>
#include<string.h>
char a[]; //要开够大小
int Next[];
void getNext(int len){
int i=,j=-;
Next[]=-;
while(i<len){
if(j==-||a[i]==a[j]){i++; j++; Next[i]=j; }
else j=Next[j];
}
}
int main()
{
int n,cir,len;
scanf("%d",&n);
while(n--)
{
scanf("%s",a);
len=strlen(a);
getNext(len);
cir=len-Next[len]; //循环节
if(len!=cir&&len%cir==)
printf("0\n");
else
printf("%d\n",cir-len%cir);
}
return ;
}
【KMP】Cyclic Nacklace的更多相关文章
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
- 【kmp】似乎在梦中见过的样子
参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...
- HDU3746 Cyclic Nacklace 【KMP】
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【POJ2752】【KMP】Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
随机推荐
- MySQL批量更新死锁案例分析--转载
问题描述 在做项目的过程中,由于写SQL太过随意,一不小心就抛了一个死锁异常,如下: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackE ...
- Linux控制台下的快捷键
Linux控制台(文本模式)下提高工作效率的快捷键 在Linux环境里,有一些按键有特殊的含意.# Ctrl-U: 擦除一行光标前面的部分.# Ctrl-H: 擦除光标前面的一个字符.# Ctrl-D ...
- Linux服务的管理
1.Linux服务的介绍 系统服务 --某些服务的服务的对象是Linux系统本身,或者Linux系统系统用户,这类服务我们称为系统服务(System Service) 网络服务 --提供给网络中的其他 ...
- 关于 submit 按钮的 onclick 验证事件,第一次验证失败,第二次 submit 按钮失效的原因解析
今天在做一个生成订单的简单验证页面 主要功能:实现点击提交按钮后,先在页面上用JavaScript验证信息是否输入完全,不完全不允许提交,待输入完全后,才可以提交 页面:如下页面,可以看到,当文本框有 ...
- python面对对象编程------3:写集合类的三种方法
写一个集合类的三种方法:wrap,extend,invent 一:包装一个集合类 class Deck: def __init__( self ): self._cards = [card6(r+1, ...
- spring配置文件位置
参考http://name327.iteye.com/blog/1628884
- (转)XML CDATA是什么?
解析数据 XML 解析器通常情况下会处理XML文档中的所有文本. 当XML元素被解析的时候,XML元素内部的文本也会被解析: <message>This text is also pars ...
- JDBC连接Oracle数据库时出现的ORA-12505错误及解决办法
转载至http://www.blogjava.net/itspy/archive/2007/12/20/169072.html Oracle 问题描述:今天使用jdbc连接oracle 10.2.0. ...
- WPFPath素材
放在Viewbox中可固定比例 <!--五角星--> <Canvas Width="100" Height="100"> <Pat ...
- 解决secureCRT数据库里没有找到防火墙 '无'问题,转自:http://jingyan.baidu.com/article/9989c74601274bf649ecfe74.html
中文版的secureCRT由于汉化的问题(把null翻译成无了),导致每次打开都会有个防火墙的错误提示:数据库里没有找到防火墙 '无' 此会话将尝试不通过防火墙进行连接.出现这个错误的原因是在secu ...