URAL 1297 suffix array: Find the longest palindrome string

Source: Internet
Author: User
Tags bitset cmath

thinking: this afternoon and then has been WA, the back of the discuss, there are the number of groups: ABCDEFDCBA, this I output ABCD, so wrong.

Then know that they wrote the suffix array to the palindrome string has a bug, and then do not know how to change.

And then look at the puzzle, inside are used RMQ first pre-treatment arbitrarily two suffixes of the longest public prefix, because do not know this, so I looked at the afternoon, well ...

Then understand the RMQ and suffixes together with the time only to find the fact that there is no RMQ can also, just to special deal with the above this has not been the example can be, haha ... Wit......

Solution One:

This is not done by RMQ:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <queue> #include <set> #include <cmath> #include <bitset> #define MEM (A, B) memset (a , B,sizeof (a)) #define Lson i<<1,l,mid#define Rson i<<1|1,mid+1,r#define Llson j<<1,l,mid#define Rrson j<<1|1,mid+1,r#define INF 0x7fffffff#define maxn 4010using namespace Std;typedef long Long ll;typedef Unsigne    D Long Long ull;void radix (int *str,int *a,int *b,int n,int m) {static int count[maxn];    MEM (count,0);    for (int i=0; i<n; i++) ++count[str[a[i]];    for (int i=1; i<=m; i++) count[i]+=count[i-1]; for (int i=n-1; i>=0; i--) b[--count[str[a[i]]]]=a[i];}    void suffix (int *str,int *sa,int n,int m) {static int rank[maxn],a[maxn],b[maxn];    for (int i=0; i<n; i++) rank[i]=i;    Radix (str,rank,sa,n,m);    rank[sa[0]]=0;    for (int i=1; i<n; i++) rank[sa[i]]=rank[sa[i-1]]+ (Str[sa[i]]!=str[sa[i-1]]); for (int i=0; 1<<i<n;            i++) {for (int j=0; j<n; J + +) {a[j]=rank[j]+1;            b[j]=j+ (1<<i) >=n?0:rank[j+ (1<<i)]+1;        Sa[j]=j;        } radix (b,sa,rank,n,n);        Radix (a,rank,sa,n,n);        rank[sa[0]]=0; for (int j=1; j<n; j + +) rank[sa[j]]=rank[sa[j-1]]+ (a[sa[j-1]]!=a[sa[j]]| |    B[SA[J-1]]!=B[SA[J]]);    }}void calcheight (int *str,int *sa,int *h,int n) {static int rank[maxn];    int k=0;    h[0]=0;    for (int i=0; i<n; i++) rank[sa[i]]=i;        for (int i=0; i<n; i++) {k=k==0?0:k-1;        if (Rank[i]) while (str[i+k]==str[sa[rank[i]-1]+k]) k++;        else k=0;    H[rank[i]]=k;    }}int A[maxn],sa[maxn],height[maxn];int Main () {string S;        while (cin>>s) {string str;        for (int i=s.size ()-1; i>=0; i--) str+=s[i];        str=s+ "#" +STR;        Copy (Str.begin (), Str.end (), a);        int n=str.size ();        Suffix (a,sa,n,n+256); CalchEight (a,sa,height,n);        int Len=0,pos=1;            for (int i=1; i<n; i++) if (sa[i]+sa[i-1]==n-height[i]&& ((Sa[i]<s.size ())! = (Sa[i-1]<s.size ())))                    {if (Height[i]>len) {len=height[i];                Pos=min (Sa[i],sa[i-1]);            } else if (Height[i]==len) pos=min (Pos,min (sa[i],sa[i-1]));        } if (len>1) cout<<str.substr (Pos,len) <<endl;    else cout<<s[0]<<endl; } return 0;} /*ABCDDCBAEFDCBAABCDEFDCBA//If there is no sa[i]+sa[i-1]==n-height[i] This sample output abcdabcddbcaaaaaaaaaaa*/

Solution Two:

This is with RMQ, I was to understand the RMQ and the height array used to deal with the problem after I wrote, originally started on the right, but just started to deal with the character is wrong, and then debugging for a long time just good.

To infer the parity of a string, as a result of ... This oneself to write a string out of their own judgment on the clear, the paper also said very clearly.

What's more, even if you want to infer that a character is not the same as its symmetric character, this example will not go through: ABCEFCBA

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <queue> #include <set> #include <cmath> #include <bitset> #define MEM (A, B) memset (a , B,sizeof (a)) #define Lson i<<1,l,mid#define Rson i<<1|1,mid+1,r#define Llson j<<1,l,mid#define Rrson j<<1|1,mid+1,r#define INF 0x7fffffff#define maxn 2010using namespace Std;typedef long Long ll;typedef Unsigne    D Long Long ull;void radix (int *str,int *a,int *b,int n,int m) {static int count[maxn];    MEM (count,0);    for (int i=0; i<n; i++) ++count[str[a[i]];    for (int i=1; i<=m; i++) count[i]+=count[i-1]; for (int i=n-1; i>=0; i--) b[--count[str[a[i]]]]=a[i];}    void suffix (int *str,int *sa,int n,int m)//multiplication algorithm computes the suffix array sa{static int RANK[MAXN],A[MAXN],B[MAXN];    for (int i=0; i<n; i++) rank[i]=i;    Radix (str,rank,sa,n,m);    rank[sa[0]]=0; for (int i=1; i<n; i++) rank[sa[i]]=rank[sa[i-1]]+ (Str[sa[i]]!=str[sa[i-1]]);   for (int i=0, 1<<i<n; i++) {for (int j=0; j<n; J + +) {a[j]=rank[j]+1;            b[j]=j+ (1<<i) >=n?0:rank[j+ (1<<i)]+1;        Sa[j]=j;        } radix (b,sa,rank,n,n);        Radix (a,rank,sa,n,n);        rank[sa[0]]=0; for (int j=1; j<n; j + +) rank[sa[j]]=rank[sa[j-1]]+ (a[sa[j-1]]!=a[sa[j]]| |    B[SA[J-1]]!=B[SA[J]]);    }}void calcheight (int *str,int *sa,int *h,int *rank,int N)//find the longest common prefix array h{int k=0;    h[0]=0;    for (int i=0; i<n; i++) rank[sa[i]]=i;        for (int i=0; i<n; i++) {k=k==0?0:k-1;        if (Rank[i]) while (str[i+k]==str[sa[rank[i]-1]+k]) k++;        else k=0;    H[rank[i]]=k;    }}int A[maxn],sa[maxn],height[maxn],rank[maxn];char s[maxn];int dp[maxn][40];void RMQ_INIT (int n) {mem (dp,0);    for (int i=1; i<n; i++) dp[i][0]=height[i]; for (int j=1; (1&LT;&LT;J) <=n; J + +) for (int i=1; i+ (1<<j) -1<n; i++) dp[i][j]=min (Dp[i][J-1],dp[i+ (1<< (j-1))][j-1]);}    int RMQ (int l,int r) {L=rank[l],r=rank[r];    if (l>r) swap (L,R); l++;    Why add 1, because L is the longest common prefix to l+1 location//i.e. Height[l+1]=suffix (l+1) and suffix (l) the longest public prefix int k=log (r-l+1.0)/log (2.0); return min (dp[l][k],dp[r-(1<<k) +1][k]);}    int main () {//freopen ("1.txt", "R", stdin);        while (~SCANF ("%s", s)) {int N=strlen (s), I;        for (i=0,s[n]= ' # '; i<n; i++) s[i+n+1]=s[n-i-1];        n=n*2+1;        s[n]= ' + ';        MEM (a,0); mem (height,0);        MEM (rank,0); mem (sa,0);        Copy (s,s+n,a);        Suffix (a,sa,n,256);        Calcheight (A,sa,height,rank,n);        Rmq_init (n);//preprocessing the longest common prefix int sum=0,pos The string two positions before the suffix; for (i=0; i<n/2; i++) {int len=rmq (i,n-i-1);//String length is odd if (len*2-1>sum) sum=len*2-1,pos=            i-len+1;            if (i) {len=rmq (i,n-i);//String length is even if (len*2>sum) Sum=len*2,pos=i-len; }} for (I=pos; i<poS+sum;        i++) printf ("%c", S[i]);    Puts (""); } return 0;}



URAL 1297 suffix array: Find the longest palindrome string

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.