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. 网络编辑基础:对HTTP协议的头信息详解

    HTTP(HyperTextTransferProtocol) 是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP 协议的详细内容请参 考RFC2616.HTTP协议采用了请求/响应模型 ...

  2. ylb:创建数据库、表,对表的增查改删语句

    ylbtech-SQL Server:SQL Server-创建数据库.表,对表的增查改删语句 SQL Server 创建数据库.表,对表的增查改删语句. 1,ylb:创建数据库.表,对表的增查改删语 ...

  3. 12、NFC技术:读写NFC标签中的Uri数据

    功能实现,如下代码所示: 读写NFC标签的Uri 主Activity import cn.read.write.uri.library.UriRecord; import android.app.Ac ...

  4. NSIS学习笔记(转)

    转自:http://blog.csdn.net/lee353086/article/details/45919901 NSIS学习笔记Date:2015-05-20Author:kagulaEnv:V ...

  5. python中函数的总结之三

    1. 可变长参数 在函数中可变长参数分为两种:一种是非关键字参数,表示为元组:一种是关键字参数,表示为字典. 具体看下面的例子代码,相当于单元测试: #!/usr/bin/env python #'t ...

  6. Python filter()删除1-100内素数

    用filter()删除1-100内的素数: #!/usr/bin/env python #coding:utf-8 import math def fil(n): #定义fil函数 flag = 0 ...

  7. 9段高效率开发PHP程序的代码

    php是世界上最好的语言 在php网站开发中,大家都希望能够快速的进行程序开发,如果有能直接使用的代码片段,提高开发效率,那将是起飞的感觉.今天由杭州php工程师送出福利来了,以下9段高效率开发PHP ...

  8. HW7.15

    public class Solution { public static void main(String[] args) { double[][] set1 = {{1, 1}, {2, 2}, ...

  9. HDU1243:反恐训练营

    题目链接:反恐训练营 题意:本质上是求最大公共子序列,然后加上一个权值 分析:见代码 //公共子序列问题 //dp[i][j]表示前s1的前i个与s的前j个匹配得到的最大公共子序列 #include& ...

  10. POJ2406----Power Strings解题报告

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 43514   Accepted: 18153 D ...