timus 1297 Palindrome后缀数组,求最长公共回文字

    技术2022-05-20  57

     

    1297. Palindrome

    Time Limit: 1.0 second Memory Limit: 16 MB 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. 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 output ArozaupalanalapuazorA Problem Author: Eugene Krokhalev Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004) Tags:  string algorithms     (
    hide tags for unsolved problems
    )
    Printable version     Statistics     Discuss     Submit solution

    #include<iostream> #include<cstring> #include<cstdio> #include<math.h> using namespace std; const int maxn=2010; int n,w[maxn],wa[maxn],wb[maxn],wv[maxn],a[maxn],sa[maxn],ran[maxn],height[maxn],f[maxn][20]; int cmp(int *r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void da(int *r,int *sa,int n,int m) { int i,j,p,*x=wa,*y=wb,*t; for (i=0; i<m; i++) w[i]=0; for (i=0; i<n; i++) w[x[i]=r[i]]++; for (i=1; i<m; i++) w[i]+=w[i-1]; for (i=n-1; i>=0; i--) sa[--w[x[i]]]=i; for (p=1,j=1; p<n; 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; for (i=0; i<m; i++) w[i]=0; for (i=0; i<n; i++) w[wv[i]=x[y[i]]]++; for (i=1; i<m; i++) w[i]+=w[i-1]; for (i=n-1; i>=0; i--) sa[--w[wv[i]]]=y[i]; 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++; } return; } void cal(int *r,int *sa,int n) { int i,j,k=0; for (i=1; i<=n; i++) ran[sa[i]]=i; for (i=0; i<n; height[ran[i++]]=k) for (k?k--:0,j=sa[ran[i]-1]; r[i+k]==r[j+k]; k++); return; } int nmin(int a,int b) { if (a<b) return a; else return b; } void st(int n) { int i, j, k, m; k = (int) (log((double)n) / log(2.0)); for(i =1; i <=n; i++) f[i][0] =height[i]; //递推的初值 for(j = 1; j <= k; j++) { //自底向上递推 for(i = 1; i + (1 << j) - 1 <= n; i++) { m = i + (1 << (j - 1)); //求出中间的那个值 f[i][j] = nmin(f[i][j-1], f[m][j-1]); } } } //查询i和j之间的最值,注意i是从0开始的 int rmq(int x, int y) { int a=ran[x],b=ran[y]; if (a>b) { int t=a; a=b; b=t; } a++; int k = (int)(log(double(b-a+1)) / log(2.0)),t2; //用对2去对数的方法求出k return t2 = nmin(f[a][k], f[b - (1<<k) + 1][k]); } int main() { char s[maxn]; cin >> s; n=strlen(s); int i,ans=0,k=0; for (i=0; i<n; i++) a[i]=int(s[i]); a[n]=1; for (i=0; i<n; i++) a[i+n+1]=int(s[n-1-i]); a[2*n+1]=0; da(a,sa,2*n+2,123); cal(a,sa,2*n+1); //for(int i=1;i<=2*n+1;i++) printf("%d ",height[i]); // printf("/n"); st(2*n+1); for (i=0; i<n; i++) { int t=rmq(i,2*n-i)*2-1; //printf("t=%d/n",t); if (t>ans) { ans=t; k=i; } if (i>0) { t=rmq(i,2*n-i+1)*2; if (t>ans) { ans=t; k=i; } } } if (ans%2!=0) { for (i=k-ans/2; i<=k+ans/2; i++) cout<<s[i]; } else { for (i=k-ans/2; i<k+ans/2; i++) cout<<s[i]; } cout<<endl; return 0; }  


    最新回复(0)