Test instructions: Finding the longest palindrome string
This has a special O (n) set of board algorithms, but as an exercise or a suffix array to solve it
Simply reverse the same string (in the middle with a sufficiently small and non-appearing character), then enumerate the midpoint of the palindrome, and continue to solve the forward and backward calculation of \ (lcp\) .
There's a bug in the template that's been changed.
One notable place is the problem of endpoint selection when the palindrome's length is odd and even enumerated, and the specific view of chestnuts
Abcccd
An odd enumeration should be ABCccd#dcccBA
An even enumeration should be ABCccD#DCCcba
Two substring enumeration The distance between the first end and the # is equal or a difference of one, the list is when the odd-side enumeration \ (i\) , the other substring starts with \ (2n+2-i\), even end is \ (2n+3-i\)
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#define REP (i,j,k) for (register int i=j;i<=k;i++)#define RREP (i,j,k) for (register int i=j;i>=k;i--)#define EREP (i,u) for (register int i=head[u];~i;i=nxt[i])#define IIN (a) scanf ("%d", &a)#define Lin (a) scanf ("%lld", &a)#define DIN (a) scanf ("%lf", &a)#define S0 (a) scanf ("%s", a)#define S1 (a) scanf ("%s", a+1)#define PRINT (a) printf ("%lld", (ll) a)#define ENTER Putchar ('\n ')#define Blank Putchar (")#define PRINTLN (a) printf ("%lld\n ", (ll) a)#define IOS ios::sync_with_stdio (0)using namespaceStdConst intMAXN =1e5+11;Const intOO =0x3f3f3f3f;Const DoubleEPS =1e-7;typedef Long LongLl;ll Read () {ll x=0, f=1;Register CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 '){if(ch=='-') f=-1; Ch=getchar ();} while(ch>=' 0 '&&ch<=' 9 ') {x=x*Ten+ch-' 0 '; Ch=getchar ();}returnX*f;}CharSTR[MAXN];intNstructsa{intRANK[MAXN],SA[MAXN],TSA[MAXN],A[MAXN],B[MAXN];intCNTA[MAXN],CNTB[MAXN];intheight[maxn],best[maxn][ -],n;voidGetintnn) {n=nn; Rep (I,0,666) cnta[i]=0; Rep (I,1, n) cnta[str[i]]++; Rep (I,1,666) cnta[i]+=cnta[i-1]; Rrep (I,n,1) Sa[cnta[str[i]]--]=i; rank[sa[1]]=1; Rep (I,2, N) {if(Str[sa[i]]==str[sa[i-1]]) {Rank[sa[i]]=rank[sa[i-1]]; }Else{rank[sa[i]]=1+rank[sa[i-1]]; } } for(intL=1; rank[sa[n]]<n;l<<=1) {Rep (I,1, N) cnta[i]=cntb[i]=0; Rep (I,1, n) cnta[a[i]=rank[i]]++; Rep (I,1, N) cntb[b[i]= (i+l<=n? RANK[I+L]:0)]++; Rep (I,1, N) cnta[i]+=cnta[i-1],cntb[i]+=cntb[i-1]; Rrep (I,n,1) Tsa[cntb[b[i]]--]=i; Rrep (I,n,1) Sa[cnta[a[tsa[i]]]--]=tsa[i]; rank[sa[1]]=1; Rep (I,2, N) {BOOLFlag=a[sa[i]]==a[sa[i-1]]&&b[sa[i]]==b[sa[i-1]]; Flag=!flag; Rank[sa[i]]=rank[sa[i-1]]+flag; } } }voidHT () {intj=0; Rep (I,1, N) {if(j) j--; while(Str[i+j]==str[sa[rank[i]-1]+J]) J + +; Height[rank[i]]=j; } }voidRMQ () {Rep (I,1, N) best[i][0]=height[i]; for(intI=1;(1<<i) <=n;i++) { for(intj=1; j+ (1<<i)-1<=n;j++) {best[j][i]=min (best[j][i-1],best[j+ (1<< (i-1))][i-1]); } } }intQueryintLintR) {if(L==R)return-oo;if(l>r) swap (L,R); l++;intK=LOG2 (r-l+1);returnMin (best[l][k],best[r-(1<<K)+1][k]); }}sa;CharSSTR[MAXN];intMain () { while(~S1 (str)) {N=strlen (str+1); Rep (I,1N+1) Sstr[i]=str[i]; Reverse (sstr+1, Sstr+1+N); Str[n+1]=1; str[n+2]=0; strcat (str+1, Sstr+1); str[2*n+2]=0;intnn=2*n+1; Sa.get (NN); Sa.ht (); SA.RMQ ();intans=1, pos=1; Rep (I,1, N) {if(1){intOdd=sa.query (SA. Rank[i],sa. rank[2*n+2-I.]);if(ans<2*odd-1) ans=2*odd-1, pos=i-odd+1; }intEven=sa.query (SA. Rank[i],sa. rank[2*n+3-I.]);if(ans<2*even) ans=2*even,pos=i-even; }//cout<<ans<<endl;str[ans+pos]=0;p rintf ("%s\n", Str+pos); memset (str,0,sizeofSTR); memset (SSTR,0,sizeofSSTR); }return 0;}
URAL-1297 suffix array for LCP application