[POJ2752] Seek the Name,Seek the Fame
来源:互联网 发布:金和网络摄像头 编辑:程序博客网 时间:2024/05/16 11:22
题目描述
给你若干个字符串,求其前缀=后缀的长度,从小到大依次输出。例如ababcabab,依次输出2(ab),4(abab),9(ababcabab)。
输入格式
输入文件包含多组测试数据,每组数据为一行一个字符串S,仅有小写字母组成。1<=Length of S<=400000。
输出格式
对于每组测试数据输出一行按照长度递增的顺序输出。
样例数据
样例输入
ababcababababcabab
aaaaa
样例输出
2 4 9 18
1 2 3 4 5
题目分析
KMP
根据next数组性质可知,由next断开的左右前后缀相同,故while递归next即可
源代码
#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj;}string s;int len,Next[400005],ans[400005],cnt=0;void Get_Next(string s) { int i=1,j=0; Next[1]=0; while(i<=len) if(j==0||s[i]==s[j])Next[++i]=++j; else j=Next[j];}int main() { ios::sync_with_stdio(false); while(cin>>s) { len=s.length(); s=' '+s; cnt=0; Get_Next(s); int k=len+1; while(Next[k]>1) { ans[++cnt]=Next[k]-1; k=Next[k]; } for(int i=cnt; i>=1; i--)printf("%d ",ans[i]); printf("%d\n",len); } return 0;}
0 0
- POJ2752 Seek the Name, Seek the Fame
- POJ2752:Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 seek the name seek the fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752-Seek the Name, Seek the Fame
- poj2752 Seek the Name,Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 seek the name,seek the fame
- POJ2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752-Seek the Name, Seek the Fame
- [POJ2752] Seek the Name,Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- 【POJ2752】seek the name,seek the fame
- Linux中环境变量文件及配置
- 信号阻塞未达pending原理机制讲解
- SQL中Case的使用方法
- cf623a
- 信号未达代码讲解
- [POJ2752] Seek the Name,Seek the Fame
- 获取ios设备当前网络状态
- JavaScript基础
- Codeforces Round #304 (Div. 2)-E. Soldier and Traveling
- 《程序员的思维修炼 开发认知潜能的九堂课》(一)
- 支付宝用AR集五福,VR和MR怎么看?
- ORB-SLAM(1) --- 让程序飞起来
- shiro用户授权的时候,在springmvc.xml配置出现The prefix "aop" for element "aop:config" is not bound.
- PAT---A1001. A+B Format (20)