URAL 1297. palindrome (suffix array to find the longest palindrome substring)

Source: Internet
Author: User

Title Link: http://acm.timus.ru/problem.aspx?space=1&num=1297


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
input Output
Thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora
Arozaupalanalapuazora

The code is as follows:

#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include < algorithm>using namespace std; #define N 2047int Wa[n], wb[n], wv[n], Ws1[n];int rank1[n]; Rank array int height[n]; Rank the longest common prefix of the next two suffixes int sa[n]; The SA is a suffix array, and N suffixes are sorted from small to large to the beginning of the ordered suffix Char s[n];int a[n], n;int dp[n][47];int minn (int a,int b) {return a>b?b:a;} int cmp (int *r,int a,int b,int l) {return r[a]==r[b] && r[a+l]==r[b+l];}    void Get_sa (int *r, int *sa, int n, int m)//multiplication algorithm {int i,j,p,*x=wa,*y=wb,*t;    for (i=0; i<m; i++) ws1[i]=0;    for (i=0; i<n; i++) ws1[x[i]=r[i]]++;    for (I=1; i<m; i++) ws1[i]+=ws1[i-1]; for (i=n-1; i>=0; i--) sa[--ws1[x[i]]]=i;        Sorts a string of length 1 for (p=1,j=1; p<n; j*=2,m=p) {for (p=0,i=n-j; i<n; i++) y[p++]=i; for (i=0; i<n; i++) if (sa[i]>=j) y[p++]=sa[i]-j;        The second keyword sort results for (i=0; i<n; i++) wv[i]=x[y[i];        for (i=0; i<m; i++) ws1[i]=0;     for (i=0; i<n; i++) ws1[wv[i]]++;   for (I=1; i<m; i++) ws1[i]+=ws1[i-1]; for (i=n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i]; First keyword sort for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p-1:p++; Update rank array} return;    void Get_height (int *r, int *sa, int n)//height array {int I, j, k=0;    for (I=1; i<=n; i++) rank1[sa[i]]=i;    For (i=0, i<n; height[rank1[i++]]=k) for (k?k--:0,j=sa[rank1[i]-1]; r[i+k]==r[j+k]; k++); return;}    void RMQ () {memset (dp,127,sizeof (DP));    for (int i = 1; I <= n*2+1; i++) dp[i][0] = Height[i]; for (int j = 1; (1<<j) <= 2*n+1; J + +) for (int i = 1; i+ (1<<j)-1 <= 2*n+1; i++) DP[I][J] = Minn (dp[i][j-1],dp[i+ (1<< (j-1))] [j-1]);}    int LCP (int l,int R)//for the longest public prefix {int a = Rank1[l], B = rank1[r];    if (a > B) swap (b);    a++;    int t= (int) (log (double (b-a+1))/log (2.00)); Return Minn (dp[a][t],dp[b-(1<<t) +1][t]);}    int main () {int res; while (~SCANF ("%s",s) {int maxx = 0;        n = strlen (s);        for (int i = 0; i < n; i++) a[i] = (int) s[i];        A[n] = 1;        for (int i = 0; i < n; i++) a[i+n+1] = (int) s[n-i-1];        int LEN = 2*n+1;        A[len] = 0;        Get_sa (a,sa,len+1,320);        Get_height (A,sa,len);        RMQ ();        int anslen = 0, idx = 0;            for (int i = 0; i < n; i++) {int t = LCP (I,LEN-I-1);                if (2*t-1 > Anslen)//More Forget once center letter {Anslen = 2*t-1;            idx = i;                } if (s[i] = = S[i+1]) {t = LCP (I+1,LEN-I-1);                    if (2*t > Anslen) {anslen = 2*t;                idx = i;        }}} idx = idx-(anslen-1)/2;        for (int i = 0; i < Anslen; i++) printf ("%c", S[idx+i]);    printf ("\ n"); } return 0;}


URAL 1297. palindrome (suffix array to find the longest palindrome substring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.