POJ 1961 Period(next数组求循环节)
2017-08-20 17:10
316 查看
Period
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 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 Source Southeastern Europe 2004 |
给你一个字符串,让你求长度为2到len的前缀中有没有可以循环的串。求出前缀长度和循环次数。
POINT:
nxt数组求循环节
1.http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int N = 1000000+4;
int nxt
;
void prenxt(char s[])
{
int l=strlen(s);
int i=0,j;
j=nxt[0]=-1;
while(i<l)
{
while(j!=-1&&s[i]!=s[j]) j=nxt[j];
nxt[++i]=++j;
}
}
int main()
{
int n;
int o=0;
while(~scanf("%d",&n)&&n)
{
char s
;
scanf("%s",s);
prenxt(s);
printf("Test case #%d\n",++o);
for(int i=2;i<=n;i++)
{
if(i%(i-nxt[i])==0&&nxt[i]!=0)
{
printf("%d %d\n",i,i/(i-nxt[i]));
}
}
printf("\n");
}
}
相关文章推荐
- POJ - 1961 Period(next数组求循环节)
- POJ 1961 Period KMP(next数组应用)
- POJ 1961 Period KMP算法之next数组的应用
- LA 3026 && POJ 1961 Period (利用kmp中的next数组找最小的循环节 )
- poj 1961 Period (kmp next 数组的应用)
- poj 1961 Period 【KMP-next前缀数组的应用】
- POJ 1961 Period KMP next数组的应用
- poj 1961 Period(未优化KMP的next数组)
- POJ 1961 Period KMP算法next数组的应用
- POJ 1961 Period 还是next数组的含义、
- poj 1961 Period [kmp, next数组的用法]
- Poj-1961 Period(next数组应用)
- POJ - 1961 Period解题报告(kmp next数组巧用)
- POJ 1961 Period(KMP next数组巧用)
- POJ - 2406 Power Strings —— next数组求循环节
- POJ - 1961 Period(nex数组的运用)
- poj 1961 2406 让你彻底理解KMP的next[]数组的两个题目
- POJ - 2752 Seek the Name, Seek the Fame —— next数组求循环节
- POJ 1961 Period kmp求循环节
- poj 1961(next数组的循环节)