码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2752(拓展KMP模板题)

时间:2018-09-15 21:21:02      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:aaa   turn   algorithm   input   esc   incr   ref   possible   order   

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24399   Accepted: 12731

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:) 

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. 

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

技术分享图片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn=400010;
 6 
 7 int nex[maxn];
 8 int extend[maxn];
 9 char buf[maxn];
10 
11 void Get_Next(const char T[])
12 {
13     int len=strlen(T);
14     int j=0;
15     nex[0]=len;
16     while(j+1 < len && T[j+1] == T[j])
17         j++;
18     nex[1]=j;
19     int k=1;
20     for(int i=2;i < len;++i)
21     {
22         int P=nex[k]+k-1;
23         int L=nex[i-k];
24         if(i+L <= P)
25             nex[i]=L;
26         else
27         {
28             j=max(0,P-i+1);
29             while(i+j < len && T[i+j] == T[j])
30                 j++;
31             nex[i]=j;
32             k=i;
33         }
34     }
35 }
36 void Get_Extend(const char S[],const char T[])
37 {
38     Get_Next(T);
39     int n=strlen(S);
40     int m=strlen(T);
41     int j=0;
42     while(j < n && j < m && S[j] == T[j])
43         j++;
44     extend[0]=j;
45     int k=0;
46     for(int i=1;i < n;++i)
47     {
48         int P=extend[k]+k-1;
49         int L=nex[i-k];
50         if(i+L <= P)
51             extend[i]=L;
52         else
53         {
54             j=max(0,P-i+1);
55             while(i+j < n && j < m && S[i+j] == T[j])
56                 j++;
57             extend[i]=j;
58             k=i;
59         }
60     }
61 }
62 
63 int main()
64 {
65     while(~scanf("%s",buf))
66     {
67         Get_Extend(buf,buf);
68         int len=strlen(buf);
69         for(int i=len-1;i > 0;--i)
70             if(i+extend[i] == len)
71                 printf("%d ",extend[i]);
72         printf("%d\n",len);
73     }
74     return 0;
75 }
View Code

 

poj 2752(拓展KMP模板题)

标签:aaa   turn   algorithm   input   esc   incr   ref   possible   order   

原文地址:https://www.cnblogs.com/violet-acmer/p/9652216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!