题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746

KMP算法——

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=200005;
const LL II=100000000;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); int next[N],len,nextval[N];
char str[N]; void getnext(char *p)
{
int j=0,k=-1;
next[0]=-1;
while(j<len)//len是p的长度
{
if(k==-1||p[j]==p[k])
{
j++; k++;
next[j]=k;
}
else
k=next[k];
}
} void getnextval(const char *p)
{
int i = 0,j =-1;
nextval[0]=-1;
while(i!=len)
{
if(j==-1||p[i]==p[j])
{
++i;++j;
if(p[i]!=p[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else
j=nextval[j];
}
} int main()
{
int i,j,T;
cin>>T;
while(T--)
{
scanf("%s",str);
len=strlen(str);
getnext(str);
int min_repeat=len-next[len];
if(min_repeat==len)
printf("%d\n",len);
else if(len%min_repeat==0)
printf("0\n");
else
printf("%d\n",min_repeat-len%min_repeat);
}
return 0;
}

HDU 3746 Cyclic Nacklace KMP的更多相关文章

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

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

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

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

  3. hdu 3746 Cyclic Nacklace KMP循环节

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

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

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

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

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

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

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

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

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

  8. HDU 3746 Cyclic Nacklace (用kmp求循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. cocos2d-X-3.X 场景与层

    1场景与层的相关函数 1. void runWithScene(Scene * scene). 该函数可以运行场景.只能在启动第一个场景时调用该函数.如果已经有一个场景运行,则不能调用该函数. 2.  ...

  2. 星际反作弊2.3for-win7-xp-win8-win10

    星际反作弊2.3for-win7-xp-win8-win10 下载地址 http://wj800.com/rar/scfzb.zip

  3. kafka初探

    http://www.infoq.com/cn/articles/kafka-analysis-part-1

  4. 【玩转Ubuntu】08. Linux报错:Syntax error: "(" unexpected解决办法

    问题: 在MAC上写了一段shell脚本,放到Ubuntu上运行总是报下面这个错误,单步调试都是对的,就是直接运行会报错. bixiaopeng@ubuntu:~/package$ sh packag ...

  5. USACO chapter1

    几天时间就把USACO chapter1重新做了一遍,发现了自己以前许多的不足.蒽,现在的程序明显比以前干净很多,而且效率也提高了许多.继续努力吧,好好的提高自己.这一章主要还是基本功的训练,没多少的 ...

  6. HDU 2149-Public Sale(巴什博奕)

    Public Sale Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. .NET WIN7+IIS 7.5下URLRewriter组件伪静态设置

    原文地址:WIN7+IIS 7.5伪静态的设置 --------------------------------------------------------偶是分割线君-------------- ...

  8. ajax验证码检测

    1.验证码文件 <%@ page language="java" pageEncoding="UTF-8"%> <%@ page conten ...

  9. codeforces 626E. Simple Skewness 三分

    题目链接 给n个数, 让你去掉一些数, 使得剩下的数的平均值-中位数的差值最大. 先将数组排序, 然后枚举每一个数作为中位数的情况, 对于每个枚举的数, 三分它的左右区间长度找到一个平均值最大的情况, ...

  10. A Byte of Python 笔记(12)python 标准库:sys、os,更多内容

    第14章 python 标准库 Python标准库是随Python附带安装的,它包含大量极其有用的模块. sys 模块 sys 模块包含系统对应的功能.如 sys.argv 列表包含命令行参数. # ...