题目地址: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. hadoop之hdfs学习

    简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...

  2. 详解new/delete(整合)

    C++中内存的动态分配与管理永远是一个让C++开发者头痛的问题,本文通过对C++中内存的动态分配释放的基本原理的介绍,让读者朋友能对C++中的内存的动态分配与释放有较为深入的理解,从而更好驾驭C++程 ...

  3. Control的Invoke和BeginInvoke详解

    (一)Control的Invoke和BeginInvoke 我们要基于以下认识: (1)Control的Invoke和BeginInvoke与Delegate的Invoke和BeginInvoke是不 ...

  4. hdu 4033 Regular Polygon 计算几何 二分+余弦定理

    题目链接 给一个n个顶点的正多边形, 给出多边形内部一个点到n个顶点的距离, 让你求出这个多边形的边长. 二分边长, 然后用余弦定理求出给出的相邻的两个边之间的夹角, 看所有的加起来是不是2Pi. # ...

  5. Highchart 改编风力风向图

    基于highchart修改而成的风力风向图 1.替换highchart.js drawpoints方法,替换内容如下: drawPoints: function() { var a, b = this ...

  6. MyEclipse启动时报 Unable to acquire application service. Ensure that the org.eclips

    今天MyEclipse启动时报如下错误: !SESSION 2012-02-12 11:32:55.198 ---------------------------------------------- ...

  7. 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  8. linux下emacs安装

    1.下载地址:http://ftp.gnu.org/pub/gnu/emacs/ 下载文件:emacs-24.2.tar.gz 步骤: 一.安装依赖文件:  (先进入root:终端中输入 su -) ...

  9. 下 面 这 条 语 句 一 共 创 建 了 多 少 个 对 象 : String s="a"+"b"+"c"+"d";

    javac 编译可以对字符串常量直接相加的表达式进行优化, 不必要等到运行期去进行加法运算处理, 而是在编译时去掉其中的加号, 直接将其编译成一个这些常量相连的结果.题目中的第一行代码被编译器在编译时 ...

  10. HDU2008-数值统计

    描述: 统计给定的n个实数中,负数.零和正数的个数. 代码: #include<stdio.h> #include<string.h> #include<iostream ...