标签:des style blog http color io os ar java
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 10000; 18 int rk[maxn],lcp[maxn],sa[maxn],wb[maxn],wd[maxn],wv[maxn]; 19 int st[maxn][25]; 20 bool cmp(int *r,int i,int j,int k){ 21 return r[i] == r[j] && r[i+k] == r[j+k]; 22 } 23 void da(int *r,int *sa,int n,int m) { 24 int i,k,p,*x = rk,*y = wb; 25 for(i = 0; i < m; ++i) wd[i] = 0; 26 for(i = 0; i < n; ++i) wd[x[i] = r[i]]++; 27 for(i = 1; i < m; ++i) wd[i] += wd[i-1]; 28 for(i = n-1; i >= 0; --i) sa[--wd[x[i]]] = i; 29 30 for(p = k = 1; p < n; k <<= 1,m = p) { 31 for(p = 0, i = n - k; i < n; ++i) y[p++] = i; 32 for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k; 33 34 for(i = 0; i < m; ++i) wd[i] = 0; 35 for(i = 0; i < n; ++i) wv[i] = x[y[i]]; 36 37 for(i = 0; i < n; ++i) wd[wv[i]]++; 38 for(i = 1; i < m; ++i) wd[i] += wd[i-1]; 39 for(i = n-1; i >= 0; --i) sa[--wd[wv[i]]] = y[i]; 40 41 swap(x,y); 42 x[sa[0]] = 0; 43 for(p = i = 1; i < n; ++i) 44 x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++; 45 } 46 47 } 48 void calcp(int *r,int n){ 49 for(int i = 1; i <= n; ++i) rk[sa[i]] = i; 50 int h = 0; 51 for(int i = 0; i < n; ++i){ 52 if(h > 0) h--; 53 for(int j = sa[rk[i]-1];i+h < n && j + h < n; ++h) 54 if(r[i+h] != r[j+h]) break; 55 lcp[rk[i]-1] = h; 56 } 57 } 58 void init(int n){ 59 memset(st,0,sizeof(st)); 60 for(int i = 1; i < n; ++i) st[i][0] = lcp[i]; 61 for(int i = 1; 1<<i < n; ++i){ 62 for(int j = 1; j + (1<<i) <= n; j++){ 63 st[j][i] = min(st[j][i-1],st[j+(1<<(i-1))][i-1]); 64 } 65 } 66 } 67 int query(int s,int t){ 68 s = rk[s]; 69 t = rk[t]; 70 if(s > t) swap(s,t); 71 if(t > s) t--; 72 int r = log2(t - s + 1); 73 return min(st[s][r],st[t-(1<<r)+1][r]); 74 } 75 char str[maxn],tmp[maxn]; 76 int r[maxn]; 77 int main() { 78 while(gets(str)){ 79 int len = strlen(str); 80 strcpy(tmp,str); 81 str[len] = ‘#‘; 82 reverse(tmp,tmp+len); 83 strcpy(str+len+1,tmp); 84 for(int i = 0; str[i]; ++i) 85 r[i] = str[i]; 86 r[len+len+2] = 0; 87 da(r,sa,len+len+2,128); 88 calcp(r,len+len+1); 89 init(len+len+1); 90 int ans = 0,index = 0; 91 for(int i = 0; i <= len; ++i){ 92 int t = query(i,2*len-i)*2 - 1; 93 if(t > ans){ 94 ans = t; 95 index = i; 96 } 97 if(i){ 98 t = query(i,2*len-i+1)*2; 99 if(t > ans){ 100 ans = t; 101 index = i; 102 } 103 } 104 } 105 if(ans&1){ 106 for(int i = index-(ans>>1),t = index + (ans>>1); i <= t; ++i) 107 putchar(str[i]); 108 putchar(‘\n‘); 109 110 }else { 111 for(int i = index - (ans>>1),t = index+(ans>>1);i < t; ++i) 112 putchar(str[i]); 113 putchar(‘\n‘); 114 } 115 } 116 return 0; 117 }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/crackpotisback/p/4032626.html