Ural1297palindrome (suffix array +lcp)

Source: Internet
Author: User
Tags min
Find the longest palindrome substring, and this problem requires the first appearance, SA+LCP solve. Original idea: First inversion, splicing the original string and the reverse string, and then the longest common prefix of the two, the only thing to note is if there are more than one group to output the first group to appear. The original idea was wrong. Counter example: Zzzdzaadzzz. Can find their own, really not, to the Great God blog under the reading is also possible. Transmission Door

Positive solution: The first thing to do is to insert a special, impossible character between itself and the mirror. We enumerate the characters in each of the original strings and find them centered (in odd and even two cases), for example, for the characters with subscript I, when the palindrome string is odd, we are asking for the longest common prefix of the suffix of I and the 2*n-i suffix, Then according to the property of the height array into the minimum value of height[i+1]...height[2*n-i], then we think of using RMQ to find the most value of the interval.

#include <cstdio> #include <cstring> #define N (char s[n],str[n];
int rank[n<<1],rank1[n],sa[n],count[n],tmp[n],h[n],st[n][14],log[n]; inline int swap (int &x,int &y) {int t=x;x=y;y=t;} inline int min (int x,int y) {return x<y?x:y;} inline int rmq (in
	T l,int r) {int t=log[r-l+1];
return min (st[l][t],st[r-(1<<t) +1][t]);
	} inline int LCP (int x,int y) {int l=rank[x],r=rank[y];
	if (l>r) swap (l,r); l++;
Return RMQ (L,R);
	} int main () {//Freopen ("A.in", "R", stdin);
	while (scanf ("%s", str+1)!=eof) {int Ans=0,ansl=0,n=0,len=strlen (str+1);
	for (int i=1;i<=len;++i) s[++n]=str[i];
	S[++n]= ' z ' +1;for (int i=len;i>=1;--i) s[++n]=str[i];
	memset (rank,0,sizeof (rank));
	memset (Count,0,sizeof (count));
	memset (h,0,sizeof (h));
	for (int i=1;i<=n;++i) count[s[i]]=1;
	for (int i=1;i<=200;++i) count[i]+=count[i-1];
	for (int i=n;i>=1;--i) rank[i]=count[s[i]];
	int k=0;
		for (int p=1;k!=n;p<<=1) {memset (count,0,sizeof (count)); for (int i=1;i<=n;++i) count[rank[i+p]]++;
		for (int i=1;i<=n;++i) count[i]+=count[i-1];
		for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
		memset (Count,0,sizeof (count));
		for (int i=1;i<=n;++i) count[rank[tmp[i]]]++;
		for (int i=1;i<=n;++i) count[i]+=count[i-1];
		for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
		memcpy (rank1,rank,sizeof (RANK1));
		rank[sa[1]]=k=1; for (int i=2;i<=n;++i) {if (rank1[sa[i]]!=rank1[sa[i-1]]| |
			RANK1[SA[I]+P]!=RANK1[SA[I-1]+P]) ++k;
		Rank[sa[i]]=k;
	}}k=0;
		for (int i=1;i<=n;++i) {if (rank[i]==1) {h[1]=0;continue;} if (i==1| |
		H[rank[i-1]]<=1) k=0;
		if (k)--k;
		while (S[i+k]==s[sa[rank[i]-1]+k]) ++k;
	H[rank[i]]=k;
	}log[0]=-1;
	for (int i=1;i<=n;++i) log[i]=log[i>>1]+1;
	for (int i=1;i<=n;++i) st[i][0]=h[i]; for (int i=1;i<=log[n];++i) for (int j=1;j<=n;++j) if (j+ (1<<i-1) <=n) st[j][i]=min (st[j][i-1],st[j+ (1
			<<i-1)][i-1]);
	else break; for (int i=1;i<=len;++i) {//s[i] The longest palindrome/palindrome length is odd intX=LCP (N-i+1,i), a=i-x+1;x= (x<<1)-1;
		if (X>ans) ans=x,ansl=a;
		else if (X==ans) ansl=min (ansl,a);
		Palindrome length is even if (i==1) continue;
		X=LCP (n-i+2,i);a=i-x;x<<=1;
		if (X>ans) ans=x,ansl=a;
	else if (X==ans) ansl=min (ansl,a);
	} for (int i=0;i<ans;++i) Putchar (S[ansl+i]);
	Puts ("");}
return 0; }



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.