题目地址: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. C - 下沙小面的(2)

    C – 下沙小面的(2)Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uSubmit StatusDes ...

  2. C++学习之友元类和友元函数

    C++学习之友元类和友元函数       模板类声明也可以有友元,模板的友元可以分为以下几类:        1.非模板友元:        2.约束模板友元,即就是友元的类型取决于类被实例化的时候的 ...

  3. JQuery实战学习--在dreamweaver 8中配置Jquery自动提示

    最近在学习jQuery,然后在网上找到了自动提示的方法,记之. 1,首先下载jQuery_API.mxp这个扩展文件. 2,打开DW,点击命令-->扩展管理-->文件-->安装扩展, ...

  4. php以fastCGI的方式运行在iis下,遇到的文件系统权限问题及解决方法

    今天准备将一个php demo放在IIS下运行,网站在IIS下的配置是这样的: 应用程序池是集成模式下的.net framework 2.0(2.0或4.0没什么关系,因为php以fastCGI的方式 ...

  5. 为什么要for循环以及for循环的流程

    /* Name:为什么需要循环以及for循环流程 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月10日 03:16:55 Description:求1 ...

  6. linux下如何查看系统和内核版本

        1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@cro ...

  7. Android SharedPreference最佳实践

    Android提供多种方式保存应用数据,其中一种方式是SharedPreferences,使用键值对保存私有基本的数据.所有的逻辑仅基于以下三个类: SharedPreferences SharedP ...

  8. was配置oracle RAC集群的数据源

    在WebSphere中配置配置Oracle RAC集群的数据源,假设Oracle RAC双击分别为 HOST1 与 HOST2 , 端口为1521 ,服务名为 orcldbservice,则配置的UR ...

  9. rsyslog 不打印日志到/var/log/messages

    *.info;mail.none;authpriv.none;cron.none;local3.none /var/log/messages 表示 所有来源的info级别都记录到/var/log/me ...

  10. 【C++算法设计】八数码问题

    八数码问题 [题意] 编好为1~8的8个正方形滑块摆成3行3列(一个格子为空),如图所示 每次可以移动空格相邻的滑块到空格,要计算出能移动出目标局面的最小步数,如无法达到则输出-1. [分析] 我们可 ...