poj 2752 求一个字符串所有的相同前后缀

 

求一个字符串所有的相同前后缀
Sample Input

ababcababababcabab
aaaaa
Sample Output

2 4 9 18
1 2 3 4 5

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <stack>
 5 using namespace std;
 6 
 7 const int N = 400010;
 8 int next[N];
 9 char  T[N];
10 int  tlen;
11 stack<int> st ;
12 
13 void getNext()
14 {
15     int j, k;
16     j = 0; k = -1; next[0] = -1;
17     while(j < tlen)
18         if(k == -1 || T[j] == T[k])
19             next[++j] = ++k;
20         else
21             k = next[k];
22 
23 }
24 
25 
26 int main()
27 {
28     while (cin>>T)
29     {
30         tlen = strlen(T) ;
31         getNext() ;
32         int t = next[tlen] ;
33         while (t)
34         {
35             st.push(t) ;
36             t = next[t] ;
37         }
38         while(!st.empty())
39         {
40             int u = st.top() ;
41             printf("%d " , u) ;
42             st.pop() ;
43         }
44         printf("%d\n" , tlen) ;
45     }
46 
47 
48 
49     return 0;
50 }
View Code
 

转载于:https://www.cnblogs.com/mengchunchen/p/4502150.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值