Palindrome
Time Limit: 1000ms
Memory Limit: 16384KB
This problem will be judged on Ural. Original ID: 129764-bit integer IO format: %lld Java class name: (Any)
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).
Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.
So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
Your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. As NPRx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.
Input
The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.
Output
The longest substring with mentioned property. If there are several such strings you should output the first of them.
Sample Input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
Sample Output
ArozaupalanalapuazorA
Source
解题:后缀数组的应用,把原串逆序后接在未逆序的原串后,然后求LCP即可。然后就是RMQ了。
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(' '); 109 110 }else { 111 for(int i = index - (ans>>1),t = index+(ans>>1);i < t; ++i) 112 putchar(str[i]); 113 putchar(' '); 114 } 115 } 116 return 0; 117 }