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

求周期问题,简单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=1000005;
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)
{
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,ca=0;
while(scanf("%d",&len)&&len)
{
scanf("%s",str);
getnext(str);
printf("Test case #%d\n",++ca);
for(i=2;i<=len;i++)
{
int t=i-next[i];
if(i%t==0&&i/t!=1)
printf("%d %d\n",i,i/t);
}
printf("\n");
}
return 0;
}

HDU 1358 Period KMP的更多相关文章

  1. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  2. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

  3. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  4. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  6. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

  7. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  8. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  9. KMP + 求最小循环节 --- HDU 1358 Period

    Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...

随机推荐

  1. 用C++写一个简单的发布者

    节点是一个可执行程序,它连接到了ROS的网络系统中.我们将会创建一个发布者,也就是说话者节点,它将会持续的广播一个信息. 改变目录到之前所建立的那个包下: cd ~/catkin_ws/src/beg ...

  2. C++学习之指针的常见错误

    C++学习之指针的常见错误       我们在编程的过程中,有时候在使用指针的时候,删除一个指针以后一定要将这个指针设置为空指针,这是因为删除这个指针只是删除这个指针指向的地址,这个指针还真是的存在程 ...

  3. Spring Jdbc使用like模糊查询

    public List<WfConfigMVO> queryList(WfConfigMVO wfConfig) throws SysException { StringBuffer sq ...

  4. IronPython初体验和实战集合等类型转换和类型匹配

    人老了,做什么都累~学到了这么多技术,从最早C到C++再到JAVA再到C#,最终都是为了改善产品,改善系统的可维护性.众所周知C#,C++,C都是强类型语言,什么情况都得定义一些实体类来改变业务模型, ...

  5. python 常用模块及方法

    ******************** PY核心模块方法 ******************** os模块: os.remove()         删除文件 os.unlink()        ...

  6. Python中的isinstance函数

    isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo)   如果参数object是classinfo的实例,或者object是class ...

  7. jquery 工作笔记,不断整理中..

    $('input:radio[name="ruleType"]:checked').val()  //获得radio选中的值 $("input[name='Q_lastU ...

  8. Hadoop2.0安装

    http://blog.csdn.net/samhacker/article/details/18802223 http://blog.csdn.net/crazyhacking/article/de ...

  9. [转]maven入门

    http://wentao365.iteye.com/blog/903396 Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object mode ...

  10. java 获取随机数的三种方法

    方法1(数据类型)(最小值+Math.random()*(最大值-最小值+1))例:(int)(1+Math.random()*(10-1+1))从1到10的int型随数 方法2获得随机数for (i ...