POJ-2752-Seek the Name-kmp的变形
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
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
题意:求出前缀后缀一样的对应字符的位置,按递增顺序输出
prefix-suffix:前后缀的意思。。。
1 //ababcababababcabab 2 #include<stdio.h> 3 #include<iostream> 4 #include<set> 5 #include<string.h> 6 using namespace std; 7 const int N=400020; 8 9 int s[N]; 10 char t[N]; 11 int nextt[N]; 12 13 void getnext(int len)//求的是模式串的next数组 14 { 15 int i=0,j=-1; 16 nextt[0]=-1; 17 while(i<len) 18 { 19 if(j<0||t[i]==t[j]) 20 nextt[++i]=++j; 21 else 22 j=nextt[j]; 23 } 24 } 25 26 int main() 27 { 28 while(~scanf("%s",t)) 29 { 30 int len=strlen(t); 31 getnext(len); 32 // for(int i=0; i<len; i++) 33 // cout<<nextt[i]<<"**"<<endl; 34 int p=0; 35 for(int i=len; i!=0; ) 36 { 37 s[p++]=nextt[i]; 38 i=nextt[i]; 39 } 40 p--; 41 for(int i=p-1; i>=0; i--) 42 printf("%d ",s[i]); 43 printf("%d\n",len); 44 memset(s,'\0',sizeof(s)); 45 memset(t,0,sizeof(t)); 46 memset(nextt,'\0',sizeof(nextt)); 47 } 48 return 0; 49 }
分类:
字符串
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」