Problem Descriptionalice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three tuple (i,j,k) satisfy 1≤i≤j<< Span id= "mathjax-span-9" class= "Mi" >k≤ le ng th (s ) , S[i. J] and S[J+1..K] is all palindrome strings. It's easy for her. She wants to know the sum of i*k of all required three tuples. She can ' t solve it. So can I help her? The answer is very large, please output the answer mod 1000000007.
A palindrome string is a string, which is same, when the string is read from left to right as when the string was read from RI Ght to left.
Inputthe input contains multiple test cases.
Each test case contains one string. The length of string is between 1 and 1000000. String only contains lowercase letter.
Outputfor each test case output the answer mod 1000000007.
Sample INPUTAAAABC
Sample Output148
Test instructions: Find triples (i,j,k) so that [i,j] and [j+1,k] are palindrome strings. Calculate all i*k and modulo 1000000007.
Analysis: First run one side manacher. Then process 4 arrays
C[0][i]: Represents the point for subscript I, his palindrome (Palindrome string on his right) center subscript * * and
C[1][i]: Represents the point for subscript I, his palindrome (Palindrome string on his left) center subscript * * and
C[2][i]: Represents the point for subscript I, the number of palindrome (Palindrome string on his right)
C[3][i]:: Represents the point for subscript I, the number of palindrome he is in (Palindrome string on his left)
For a palindrome with the left end of X, all the k's on the right of it is c[0][x]-c[2][x]*x (think about why, exactly, the length of all the palindrome containing the x is the left end), to
X is the same as the right endpoint. Also pay attention to parity, I refer to other people's wording.
Code:
#include <cstdio>#include<cstring>#include<string>#include<algorithm>using namespaceStd;typedefLong LongLL;Const intMod=1000000007;Const intmaxn=2000010;CharORG[MAXN],S[MAXN];intP[maxn];inlineintManacher ()//Manacher Implementation Section{s[0]='$'; s[1]='#'; intlen=2; intL=strlen (org); for(intI=0; i<l;i++) {S[len++]=Org[i]; S[len++]='#'; } S[len]='@'; S[len+1]=0; intRi=0, id; for(intI=1; i<len;i++) { if(ri>i) P[i]=min (p[2*id-i],ri-i); Elsep[i]=1; while(S[i+p[i]]==s[i-p[i]]) p[i]++; if(I+p[i]>ri) {Ri=i+p[i]; id=i;} } returnlen-1;}intc[4][MAXN];voidModify (intKintLintRintV//Modify{ if(L>r)return; C[K][L]= (c[k][l]+v)%mod;//l position to add vc[k][r+1]= (c[k][r+1]-v+mod)%mod;//r+1 to subtract v because the paragraph from L to R is cumulative, but it will be lost by the time r+1 begins.}intMain () { while(SCANF ("%s", org)! =EOF) { intlen=Manacher (); memset (C,0,sizeof(C)); for(inti=len;i>=1; i--) {Modify (0, i-p[i]+1, I,i); Modify (2, i-p[i]+1I1); } for(intI=1; i<=len;i++) {Modify (1, i,i+p[i]-1, i); Modify (3, i,i+p[i]-1,1); } for(intk=0;k<4; k++) for(intI=1; i<=len;i++) c[k][i]= (c[k][i]+c[k][i-1])%MoD; intans=0; for(intI=2; i<len-1; i+=2) { inta=i,b=i+2; intLsum= (c[0][b]-(LL) c[2][b]* (b/2)%mod+mod)%MoD; intRsum= (c[1][a]-(LL) c[3][a]* (A/2)%mod+mod)%MoD; Ans= (ans+ (LL) lsum*rsum%mod)%MoD; } printf ("%d\n", ans); } return 0;}
View Code
Hdu5785-interesting (palindrome string processing)