博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【KMPnxt数组应用】POJ2752Seek the Name, Seek the Fame
阅读量:6452 次
发布时间:2019-06-23

本文共 2285 字,大约阅读时间需要 7 分钟。

Seek the Name, Seek the FameTime Limit: 2000MS        Memory Limit: 65536KTotal Submissions: 25118        Accepted: 13080DescriptionThe 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:) InputThe 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. OutputFor each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.Sample InputababcababababcababaaaaaSample Output2 4 9 181 2 3 4 5SourcePOJ Monthly--2006.01.22,Zeyuan Zhu
T

这道题就是求所有前缀,使这个前缀同时也是这个词的后缀

依旧是KMP,用手画一画即可得知只要一直一直跳nxt输出就行

1 #include
2 #include
3 #include
4 #define N 400000+1000 5 using namespace std; 6 char a[N]; 7 int nxt[N],len,ans[N],pp; 8 void getnxt() 9 {10 int j=1,k=0;11 while(j<=len)12 {13 if(k==0||a[j]==a[k])14 j++,k++,nxt[j]=k;15 else 16 k=nxt[k];17 }18 }19 int main()20 {21 while(scanf("%s",a+1)!=EOF)22 {23 memset(nxt,0,sizeof(nxt)),pp=0;24 len=strlen(a+1);25 getnxt();26 for(int i=len+1;i;i=nxt[i])27 ans[++pp]=i-1;28 //if(a[1]==a[len])printf("1 ");29 for(int i=pp;i>=1;i--)30 if(ans[i])printf("%d ",ans[i]);31 printf("\n"); 32 }33 return 0;34 }

 

转载于:https://www.cnblogs.com/Qin-Wei-Kai/p/10209058.html

你可能感兴趣的文章
POJ3304:Segments——题解
查看>>
48.EXt.Data.JsonReader()
查看>>
UML关系图
查看>>
一个action读取另一个action里的session
查看>>
leetcode 175. Combine Two Tables
查看>>
如何给一个数组对象去重
查看>>
Guava包学习-Cache
查看>>
2019-06-12 Java学习日记之JDBC
查看>>
灯箱效果(点击小图 弹出大图集 然后轮播)
查看>>
linux c 笔记 线程控制(二)
查看>>
samba服务器配置
查看>>
vue.js笔记
查看>>
【Unity3D入门教程】Unity3D之GUI浅析
查看>>
Hive 简单操作
查看>>
湘潭1247 Pair-Pair(树状数组)
查看>>
idea 不能粘贴复制问题
查看>>
IEnumerable<T>
查看>>
IntelliJ IDEA 注册码
查看>>
linux 上面配置apache2的虚拟目录
查看>>
Linux学习总结 (未完待续...)
查看>>