poj 2752 Seek the Name, Seek the Fame
来源:互联网 发布:本机端口号查询 编辑:程序博客网 时间:2024/05/29 08:53
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14821 Accepted: 7445
Description
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.
Sample Input
ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5
解题思路:求子串前缀与后缀一样的情况下,最长的长度
#include<stdio.h>#include<string.h>int next[400010],res[400010];int len;char s[400040];void getnext(){int i=0,j=-1;next[i]=j;while(i<len){if(j==-1||s[i]==s[j]) { i++; j++; next[i]=j; }else j=next[j];}}int main(){while(scanf("%s",s)!=EOF){memset(next,0,sizeof(next));int i,j;len=strlen(s);i=0;res[i++]=len;j=len;getnext();while(j!=0){if(next[j]!=0) res[i++]=next[j];j=next[j];}for(j=i-1;j>0;j--) printf("%d ",res[j]);printf("%d\n",res[j]); }return 0;}
0 0
- poj seek the name,seek the fame
- poj---Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame POJ
- Seek the Name, Seek the Fame POJ
- POJ-2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752--Seek the Name, Seek the Fame (next)
- POJ:2752Seek the Name, Seek the Fame
- POJ 2752 - Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ-2752-Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame---kmp
- LightOJ 1297 - Largest Box (二分查找)
- hdu5510 kmp+二分
- [004]微信公众账号开发教程第1篇-根据用户openID获取用户基本信息
- PHP与JavaScript在处理数组方面的不同之处
- [Unity热更新]lua基础(一)
- poj 2752 Seek the Name, Seek the Fame
- 如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题
- ArcGIS for Server内置JS Viewer的离线部署和配置
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
- 浅窥ArcGIS Data Store之两斑
- 杭电1040——As Easy As A+B(排序)
- LIS
- Hbase-1.1.2 HRegionServer全部挂掉的报错信息图片及解决办法
- 获取元素CSS值之getComputedStyle方法熟悉