【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 ...
随机推荐
- 一个Nodejs的简单计算測试程序
測试目的: 1 測试二维数组的使用 2 输出函数的使用 代码: var util = require('util'); a = 3; b = 4; c = a + b; a = []; for(i = ...
- Configuring the JA-SIG CAS Client --官方
1. for Java using Spring Configuration of the CAS Client for Java via Spring IoC will depend heavily ...
- git pull 代码很慢的问题
办公环境调整,之前开发机是和自己的电脑放同一网段内的,现在开发机放至到本地其他网段内,造成pull 代码很慢的问题,在网上查了一下 以下是原文,链接为 http://blog.sina.com.cn/ ...
- Python元组、列表--笔记
<Python3 程序开发指南> 序列包括元组和列表,首先,我们介绍元组. 元组--tuple 元组为有序的序列,元组和字符串一样也是固定的,不能替换或删除其中的任意数据项.如果需要修改应 ...
- GSON 示例代码 实用版
去除所有格式的json字符串 {"data":[{"friend":[{"address":"广州","nam ...
- 图片样式 scaleType 属性
ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType),不同值的意义区别:
- spring04 spel注入
1.创建需要的实体类对象 public class Student { //学生实体类 private String name; //姓名 private Integer age; //年龄 priv ...
- 图片跟着鼠标动js
<!DOCTYPE html><html><head> <title>duisgf</title> <meta charset=&qu ...
- Android时间戳转换为标准Datetime(yyyy-MM-dd hh:mm:ss)格式
下列函数为实现过程,已经测试通过. /// <summary> /// Android时间戳转换为标准Datetime /// </summary> /// <param ...
- php锁表
用PHP实现mysql锁表 mysql锁表,是利用相关的SQL语句 //执行SQL语句 锁掉userinfo表 $sql = "LOCK TABLES userinfo WRITE" ...