1297. Palindrometime limit:1.0 Second Memory limit:64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing? Robots Unlimited? Have infiltrated into "U.S. robotics". ? U.S. Robots? Security Service would has already started an undercover operation to establish the agent's identity, but, fortunately, t He letter describes communication channel the agent uses. He'll publish articles containing stolen data to the "Solaris" Almanac. Obviously, he'll obfuscate the data, so "Robots Unlimited" would have to use a special Descrambler ("Robots Unlimited" pa RT number NPRx8086, specifications is kept secret). Have read the letter, the "U.S. Robots" President recalled have 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 have finished work on the NPRx8086 design. So, the president have 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 h Aystack. 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 is working on a specific module, the text direction detector. Nobody else could finish that module, so the Descrambler would choose the text scanning direction at random. To ensure the correct descrambling of the "the message by NPRx8086, agent must encode" information in such a "that the RE Sulting secret message reads the same both forwards and backwards. In addition, it's reasonable to assume that the agent would be being sending a very long message, so John have 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 would remove them from the text before feeding it into the pro Gram. Inputthe input consists of a single line, which contains a string of Latin alphabet letters (no other characters would appe AR in the string). String length would not exceed characters. Outputthe longest substring with the mentioned property. If There is several such strings you should output the first of them. Sample
tr>
Input |
output |
thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora |
arozaupalanalapuazora |
|
Test instructions: Enter a string to output the longest palindrome string inside.
Procedure: suffix array such as input ABC. That constructs the string ABC#CBA. Then use the suffix array template. After initializing the RMQ, enumerate any of the points in front of the #, the odd palindrome and the even palindrome are considered separately,
Use LCP to find the longest common prefix of the suffix i,j. The biggest one gets is the longest palindrome string.
Templates from Kuangbin
#include <stdio.h> #include <iostream> #include <algorithm>using namespace std; LCP (i,j) =LCP (Suffix (rak[i), Suffix (Rak[j])///*suffix array * multiplication algorithm O (N*LOGN) * The array length to be sorted is N, put in 0~n-1, and a 0 *da on the last side (STR, n +1,sa,rak,height,,);//attention is n+1; * For example: *n = 8; *num[] = {1, 1, 2, 1, 1, 1, 1, 2, $}; Note that the last digit of NUM is 0, the other is greater than 0 *rak[] = {4, 6, 8, 1, 2, 3, 5, 7, 0};rak[0~n-1] are valid values, Rak[n] must 0 Invalid value *sa[] = {8, 3, 4, 5, 0, 6, 1, 7, 2};sa[1~n] are valid values, sa[0] must be n is an invalid value *height[]= {0, 0, 3, 2, 3, 1, 2, 0, 1};height[ 2~n] is a valid value * */const int Maxn=20010;int t1[maxn],t2[maxn],c[maxn];//The intermediate variable required by the SA array, does not need to be assigned//the string to be sorted in the S array, from s[0] to s[n-1], All S[i] with a length of N and a maximum of less than m,///except s[n-1] are larger than the 0,r[n-1]=0//function ends after the result is placed in the SA array bool cmp (int *r,int a,int b,int l) {return r[a] = = R[b] & ;& R[a+l] = = R[b+l]; } void da (int str[],int sa[],int rak[],int height[],int n,int m) {n++; int I, J, p, *x = t1, *y = t2; First round Cardinal Sort, if s maximum value is large, can be changed to quick sort for (i = 0;i < m;i++) C[i] = 0; for (i = 0;i < n;i++) C[x[i] = str[i]]++; for (i = 1;i < m;i++) C[i] + + c[i-1]; for (i = n-1;i >= 0;i--) sa[--c[x[i]] = i; for (j = 1;j <= N; J <<= 1) {p = 0; Directly using the SA array to sort the second keyword for (i = n-j; i < n; i++) y[p++] = i;//The second keyword is null for the smallest for (i = 0; i < n; i++) if (Sa[i] &G t;= j) y[p++] = sa[i]-J; So the array y is saved by the second keyword sorted by the result//radix sort the first keyword for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[i]]]++; for (i = 1; i < m;i++) C[i] + = c[i-1]; for (i = n-1; I >= 0;i--) sa[--c[x[y[i]]] = y[i]; Calculates a new x array of swaps based on the SA and X arrays; p = 1; X[sa[0]] = 0; for (i = 1;i < n;i++) X[sa[i] = CMP (y,sa[i-1],sa[i],j)? p-1:p++; if (P >= N) break; m = p;//The maximum value of the next cardinal order} int k = 0; n--; for (i = 0;i <= n;i++) rak[sa[i]] = i; for (i = 0;i < n;i++) {if (k) k--; j = Sa[rak[i]-1]; while (str[i+k] = = Str[j+k]) k++; Height[rak[i]] = k; }}int RAK[MAXN],HEIGHT[MAXN]; int RMQ[MAXN]; int Mm[maxn];int best[20][maxn];void initrmq (int n) {mm[0]=-1; for (int i=1;i<=n;i++) mm[i]= ((i& (i-1)) ==0)? Mm[i-1]+1:mm[i-1]; for (int i=1;i<=n;i++) best[0][i]=i; for (int i=1;i<=mm[n];i++) for (int j=1;j+ (1<<i) -1<=n;j++) {int a=best[i-1][j]; int b=best[i-1][j+ (1<< (i-1))]; if (rmq[a]<rmq[b]) best[i][j]=a; else best[i][j]=b; }}int askrmq (int a,int b) {int t; T=MM[B-A+1]; b-= (1<<t)-1; A=BEST[T][A];B=BEST[T][B]; Return rmq[a]<rmq[b]?a:b; } int LCP (int a,int b) {a=rak[a];b=rak[b]; if (a>b) swap (A, b); Return HEIGHT[ASKRMQ (a+1,b)]; } Char STR[MAXN]; int R[MAXN]; int SA[MAXN]; int main () {while (scanf ("%s", str) = = 1) {int len = strlen (str); int n = 2*len + 1; for (int i = 0;i < len;i++) R[i] = Str[i]; for (int i = 0;i < len;i++) R[len + 1 + i] = str[len-1-i]; R[len] = 1; R[n] = 0; Da (r,sa,rak,height,n,128); for (int i=1;i<=n;i++) RMQ[I]=HEIGHT[I];INITRMQ (n); int ans=0,st; int tmp; for (int i=0;i<len;i++) {TMP=LCP (i,n-i);//even symmetric if (2*tmp>ans) {ans=2*tmp; st=i-tmp; } TMP=LCP (i,n-i-1);//odd symmetric if (2*tmp-1>ans) {ans=2*tmp-1; st=i-tmp+1; }} str[st+ans]=0; printf ("%s\n", str+st); }return 0; }
URAL 1297 palindrome suffix array