Time Limit:10 Sec Memory limit:512 MB
Descriptionthe JYY has two strings A and b with a length of N. a twist string s (i,j,k) is made up of a substring consisting of the I character in A to the J character and a substring of the J character in B to the K character. For example, if a= ' XYZ ', b= ' UVW ', then twist the string s (#) = ' XYVW '. Jyy defines a "twisted palindrome" for one of the following situations:a palindrome string in the 1.A;a palindrome string in the 2.B;3. Or a palindrome twist string s (i,j,k)now Jyy want to find the longest twist palindrome. InputThe first line consists of a positive integer n. The second line contains an uppercase string A with a length of N. The third line contains a string B with a length of N, consisting of uppercase letters. 1≤n≤10^5. OutputThe first line of the output is an integer that represents the longest twisting palindrome string. Sample Input5
ABCDE
BAECBSample Output 5
The twist palindrome in the best scenario is shown below (the characters in the palindrome are not used.):
. Bc..
.. ECB Solution
a twist palindrome string can be split into 3 parts, the first part in a, the second part a palindrome substring of a or B, the third part in B, where the first part is flipped and the third part is the same, and there must be an optimal solution, its second part is its palindrome center outward extension to get the longest palindrome string, to A and B respectively run Manacher find each palindrome center of the longest palindrome radius after two minutes +hash can. Code
#include <cstdio>#include<algorithm>using namespacestd;#defineMN 100000#defineX 31#defineMOD 998244353Chara[mn+5],b[mn+5],s[mn*2+5];intn,p[mn+5],ha[mn+5],hb[mn+5],d[mn*2+5],ans;voidSolveChar*S) { inti,mx=0, L,r,l,r,mid,res; for(s[0]='.', i=1;i<2*n;++i) s[i]=i&1? s[i+1>>1]:'#'; for(i=1;i<2*n;++i) {D[i]=mx+d[mx]<i?0: Min (d[2*mx-i],mx+d[mx]-i); while(s[i+d[i]+1]==s[i-d[i]-1])++D[i]; if(I+d[i]>mx+d[mx]) mx=i; L=i-d[i]+2>>1; r=i+d[i]+1>>1; if(s==a)--r;Else++m; for(l=0, R=min (l1, n-r); l<=R;) {Mid=l+r>>1; if((1ll* (ha[l-1]-1ll*ha[l-mid-1]*p[mid]%mod+mod)%mod==(1LL* (hb[r+1]-1ll*hb[r+mid+1]*p[mid]%mod+mod)%mod) res=mid,l=mid+1; Elser=mid-1; } ans=max (ans,r-l+2+res*2); }}intMain () {inti,mx; scanf ("%d%s%s", &n,a+1, B +1); for(p[0]=i=1; i<=n;++i) p[i]=1ll*p[i-1]*x%MOD; for(i=1; i<=n;++i) ha[i]= (1ll*ha[i-1]*x+a[i])%MOD; for(i=n;i;--i) hb[i]= (1ll*hb[i+1]*x+b[i])%MOD; Solve (a); solve (b); printf ("%d", ans);}
[bzoj]4755: [Jsoi2016] Twist the palindrome string