Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2866    Accepted Submission(s): 1433

Problem Description
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is
one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
 
Input
The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on
it.
 
Output
For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing
order. Print a blank line after each test case.
 
Sample Input
3
aaa
12
aabaabaabaab
0
 
Sample Output
Test case #1
2 2
3 3 Test case #2
2 2
6 2
9 3
12 4
                                                     

这道题開始看了好久都没懂题意,后来对着例子看了一下,把题意大体弄懂了,就是给你一个字符串。求里面的循环节的个数。从第二个字符開始,推断是否是循环串,并求出循环的周期(出现的次数)。然后我自己想開始想到字符串匹配;还认为要用二个字符串,感觉kmp还是没理解到位;又把kmp看了一下,看大神的思路,事实上这就是对next数组的一种应用;next数组事实上保存的就是字符串的相似度;保存的是其前一个字符的前缀和后缀相等的最大值,假如next[i]的值为6,那么它的前一个字符前缀和后缀最多就有6个相等。
字符的编号从0開始。这里面就能够推出一个规律;那么if(i%(i-next[i])==0),则i前面的串为一个轮回串,当中轮回子串出现i/(i-next[i])次。
以下是ac的代码。
#include <cstdio>
#include <cstring>
int n;
int next[1000001];
char s[1000001];
void get_next()//求出next数组
{
int i=0,j=-1;
next[0]=-1;
while(i<=n)
{
if(j==-1 || s[i]==s[j])
{
++i;
++j;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
int t,j=1;
while(scanf("%d",&n)&&n)
{
scanf("%s",s);
get_next();
printf("Test case #%d\n",j++);
for(int i=2;i<=n;i++)
{
t=i-next[i]; //关键点在这里
if(i%t==0&&i/t>1)
{
printf("%d %d\n",i,i/t);
}
}
printf("\n");
}
return 0;
}

hdu oj 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 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  3. hdu 1358 Period(KMP入门题)

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

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

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

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

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

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

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

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

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

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

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

  9. [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

    前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...

随机推荐

  1. POJ 1844 Sum

    题意:给一个整数n,求当n由1到k的连续整数加或减组成时的最小的k. 解法:一开始觉得dp……后来觉得复杂度太大了……GG……百度了一下是个数学题orz. 如果n全部由加法组成,那么k可以组成k(k+ ...

  2. JavaScript/jQuery 表单美化插件小结

    Niceforms Niceforms是一款独立的表单美化工具,当前版本为2.0 官方主页:http://www.emblematiq.com/lab/niceforms/ 官方演示:http://w ...

  3. [转]Linux文件和目录操作命令

    转自:http://www.linuxdiyf.com/bbs/thread-416176-1-1.html 一.文件操作命令1.1 查看文件 Linux下查看文件的命令有很多,下面列出的几个是几乎所 ...

  4. python开源包提交到pypi社区

    为啥要提交到pypi?因为提交成功后,你今后想用你自己写的模块,只要pip install一下就可以了. 那么如何提交?请参看本篇教程 首先要确定你的包叫啥名,比如我的包叫xlutils3,既然确定了 ...

  5. C语言实现strcmp

    注意转化为unsigned char: strcmp.h #ifndef STRCMP_H #define STRCMP_H /************************************ ...

  6. Lucene 入门需要了解的东西

    全文搜索引擎的原理网上大段的内容,要想深入的学习,最好的办法就是先用一下,lucene 发展比较快,下面是写第一个demo  要注意的一些事情: 1.Lucene的核心jar包,下面几个包分别位于不同 ...

  7. HUE 安装

    1.从github网下载hue-master.zip (源代码包) 地址:https://github.com/cloudera/hue#development-prerequisites 2.安装依 ...

  8. WS之cxf的权限拦截器应用

    一.服务器端: 1.权限判断: package cn.tdtk.ws.interceptor; import java.util.List; import org.apache.cxf.binding ...

  9. 《学习OpenCV》练习题第四章第八题ab

    这道题是利用OpenCV例子程序里自带的人脸检测程序,做点图像的复制操作以及alpha融合. 说明:人脸检测的程序我参照了网上现有的例子程序,没有用我用的OpenCV版本(2.4.5)的facedet ...

  10. 40 个顶级 jQuery 图片、内容滑块和幻灯片(转)

    在这个快速发展的网络世界中,我们使用图片.内容滑块和幻灯片来给网站实现良好.有吸引力的外观.你可以吸引浏览者借助图像滑块让网站更加具有活力.使用 JavaScript 可以轻松实现轻量级的图片和内容滑 ...