HDU 4763 求最大长度的串A,使得S满足APAQA

给一个串,让你找一个子串,形如EAEBE,就是一个串在开头结尾中间各出现一次,问这个E最长是多少

Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa

Sample Output
0
0
1
1
2

 

  1 # include <cstdio>
  2 # include <cstring>
  3 using namespace std;
  4 
  5 char S[1000010] ;
  6 int next[1000010] ;
  7 int slen ;
  8 int tlen ;
  9 
 10 
 11 void getNext()
 12 {
 13     int j, k;
 14     j = 0; k = -1; next[0] = -1;
 15     while(j < slen)
 16         if(k == -1 || S[j] == S[k])
 17             next[++j] = ++k;
 18         else
 19             k = next[k];
 20 
 21 }
 22 
 23 
 24 
 25 int main ()
 26 {
 27     int T ;
 28     scanf("%d" , &T); 
 29     while (T--)
 30     {
 31         scanf("%s" , S) ;
 32         slen = strlen(S) ;
 33         getNext() ;
 34         int t = slen ;
 35         int ans = 0 ;
 36         while (t > slen/3)
 37            t = next[t] ;
 38         while(t)
 39         {
 40             tlen = t ;
 41             int i ;
 42             int flag = 1 ;
 43             
 44             for(i = t*2 ; i <= slen - t ; i++)//利用next数组的性质
 45             {
 46                 if (next[i] == tlen)
 47                 {
 48                     ans = tlen ;
 49                     flag = 0 ;
 50                     break ;
 51                 }
 52             }
 53             if (flag)
 54                t = next[t] ;
 55             else
 56                break ;
 57         }
 58         printf("%d\n" , ans) ;
 59         
 60     }
 61     
 62     
 63     return 0 ;
 64 }
 65 
 66 
 67 
 68 
 69 # include <cstdio>
 70 # include <cstring>
 71 using namespace std;
 72 
 73 char S[1000010] ;
 74 int next[1000010] ;
 75 int slen ;
 76 int tlen ;
 77 
 78 
 79 void getNext()
 80 {
 81     int j, k;
 82     j = 0; k = -1; next[0] = -1;
 83     while(j < slen)
 84         if(k == -1 || S[j] == S[k])
 85             next[++j] = ++k;
 86         else
 87             k = next[k];
 88 
 89 }
 90 
 91 bool kmp(int a , int b)
 92 {
 93 
 94     int i, j = 0;
 95 
 96 
 97     for(i = a; i <= b; i++)
 98     {
 99         while(j > 0 && S[i] != S[j])
100             j = next[j];
101         if(S[i] == S[j])
102             j++;
103         if(j == tlen)
104         {
105             return true ;
106         }
107     }
108     return false;
109 }
110 
111 int main ()
112 {
113     int T ;
114     scanf("%d" , &T); 
115     while (T--)
116     {
117         scanf("%s" , S) ;
118         slen = strlen(S) ;
119         getNext() ;
120         int t = slen ;
121         int ans = 0 ;
122         while (t > slen/3)
123            t = next[t] ;
124         while(t)
125         {
126             tlen = t ;
127             int flag = kmp(t ,slen - t - 1) ;//查看模式串是否匹配主串
128             if (flag)
129             {
130                 ans = t ;
131                 break ;
132             }
133             t = next[t] ;
134         }
135         printf("%d\n" , ans) ;
136         
137     }
138     
139     
140     return 0 ;
141 }
View Code

 

posted @ 2015-05-12 17:01  __Meng  阅读(271)  评论(0编辑  收藏  举报