FZU 1901 KMP找前后缀等串
2016-07-18 17:16
369 查看
Period II
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
FZU
1901
Description
For each prefix with length P of a given string S,if
S[i]=S[i+P] for i in [0..SIZE(S)-p-1],
then the prefix is a “period” of S. We want to all the periodic prefixs.
Input
Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.
Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.
Output
For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.
Sample Input
4oooacmacmacmacmacmafzufzufzufstostootssto
Sample Output
Case #1: 31 2 3Case #2: 63 6 9 12 15 16Case #3: 43 6 9 10Case #4: 29 12
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
FZU
1901
Description
For each prefix with length P of a given string S,if
S[i]=S[i+P] for i in [0..SIZE(S)-p-1],
then the prefix is a “period” of S. We want to all the periodic prefixs.
Input
Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.
Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.
Output
For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.
Sample Input
4oooacmacmacmacmacmafzufzufzufstostootssto
Sample Output
Case #1: 31 2 3Case #2: 63 6 9 12 15 16Case #3: 43 6 9 10Case #4: 29 12
#include <iostream> #include <cstdio> #include <algorithm> #include <string> using namespace std; int nex[1000005]; int k; int ans[1000005]; void pre(string str){ nex[0]=-1; int i=0,j=-1; while(i<str.size()){ if(j==-1||str[i]==str[j]){ j++;i++; nex[i]=j; }else j=nex[j]; } } void kmp(string str){ pre(str); int len=str.size(); int i=len-1; k=0; while(i>-1){ if(str[i]==str[len-1]) ans[k++]=len-i-1; i=nex[i]; } } int main() { std::cout.sync_with_stdio(false); string str; int n; while(cin>>n){ for(int t=1;t<=n;t++){ cin>>str; kmp(str); cout<<"Case #"<<t<<": "<<k<<endl; for(int i=1;i<k;i++) cout<<ans[i]<<' '; cout<<str.size()<<endl; } } }
相关文章推荐
- android intent 传递list或者对象
- 中国剩余定理概念及模板解释
- Android ProGuard混淆(一)
- git--本地和远程仓库的关联
- 数组的认识
- JS判断是否在微信浏览器打开
- 最大子序列
- markov 模型
- adb修改华为盒子的 value 和 name
- .NET程序员走向高端必读书单汇总
- LNA
- python魔法方法-单目运算及一般算数运算
- 表的查询(二)
- 28.View the Exhibit and examine the undo tablespace attributes. Your database instance is experien
- PagerAdapter总结
- 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC
- C++ 程序延时处理的几种方法
- hihocoder-#1338 : A Game
- html5shiv
- 关于开发板不能ping通外网IP