FZU 1901 KMP找前后缀等串
来源:互联网 发布:江南大学网络教育登陆 编辑:程序博客网 时间:2024/06/17 13:40
Period II
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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; } }}
0 0
- FZU 1901 KMP找前后缀等串
- hdu 2752 KMP(前后缀)
- poj 2752 (找既是前缀又是后缀的串的长度)kmp/拓展kmp
- fzu 1901 Period II(KMP)
- FZU 1901 Period II (KMP)
- [HDU 2594]Simpsons’ Hidden Talents[kmp求公共前后缀]
- hdu2594 Simpsons’ Hidden Talents(KMP,前后缀)
- 字符串的前中后缀表达式,以及KMP算法。
- POJ 2752 -kmp求所有公共前后缀长度
- HDU 4763 Theme Section(kmp求前后缀)
- fzu 1901 Period II (KMP)
- FZU 题目1901 Period II (KMP)
- FZU 1901 Period II (kmp)
- FZU 1901 Period II (KMP+技巧)
- FZU 1901 Period II(KMP)
- FZU 2064 找位置
- FZU - 2064 找位置
- FZU 2147(找规律)
- 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递
- CListCtrl删除选中行
- Octave基础操作Ⅱ
- 手动编译源代码安装GR GSM
- 源码安装mysql
- FZU 1901 KMP找前后缀等串
- mtk 耳机accdet检测驱动分析
- Android 应用程序窗体显示状态操作(requestWindowFeature()的应用)
- 《Linux学习》之网络环境的配置
- 关于开发板不能ping通外网IP
- Android开发中Handler的经典总结
- JS判断是否在微信浏览器打开
- html5shiv
- 单选按钮RadioButton