poj1961之KMP应用
来源:互联网 发布:软件项目建设方案 编辑:程序博客网 时间:2024/06/08 03:27
Language:
Period
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 10998 Accepted: 5063
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 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.
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
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
题目意思是给定一个长为n的字符串求字符串的前几位是循环字串并且求出循环次数
思路:对于一个长度为len的字符串,其可能周期=len-(len-next[len]),这个周期其实是多次循环后包含原串,并不是等于原串,所以要len%(len-next[len]) == 0来判断是否等于原串
这个所谓的周期是多次循环后包含原串,例如aaaabbaa=>求得周期是aaaabb
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=1000000+10;char s[MAX];int next[MAX];void get_next(char *a){int i=-1,j=0,len=strlen(a);next[0]=-1;while(j<len){if(i == -1 || a[i] == a[j])next[++j]=++i;else i=next[i];}for(int i=2;i<=len;++i){//前i个字符,i-next[i]是前i个字符的周期 if(i%(i-next[i]) == 0 && i/(i-next[i])>1)cout<<i<<' '<<i/(i-next[i])<<endl;}return;}int main(){int n,num=0;while(cin>>n,n){cin>>s;cout<<"Test case #"<<++num<<endl;get_next(s);cout<<endl;}return 0;}
- poj1961之KMP应用
- KMP以及next数组应用--POJ1961
- poj1961---KMP
- POJ1961->KMP
- poj1961-kmp的next数组的简单应用(2)
- [kmp算法next数组的应用][poj1961]Period
- 【KMP】 poj1961 Period
- poj1961 Period----KMP
- poj1961 kmp 重复串
- LA3026||poj1961(KMP)
- poj1961 Period (KMP)
- poj1961 KMP
- KMP POJ1961 Period
- [POJ1961]Period(KMP)
- poj1961 Period(KMP)
- kmp练习poj1961
- POJ1961 Period(KMP)
- kmp算法 和例题 poj1961
- smarty模板中判断一个字符包含在一个字符串里
- Special Memory Leak For IOS---drawinrect
- 电商淡季如何度过
- FFMPEG源码分析(1)--再版--持续更新
- js 返回上一页(按住ctr+鼠标点击及右键打开新标签的返回)
- poj1961之KMP应用
- ORACLE 修改表的表空间
- java——棋牌类游戏五子棋(singlewzq1.0)之二
- vector容器类型
- 遇到的不能刷新Product Flat Data
- Protobuf 的安装使用总结
- [Oracle]分割字符串返回数组
- java——棋牌类游戏五子棋(webwzq1.0)之一(基础)
- gdb