Back to Yoshifumi in dealing with palindrome really better than manacher to be more useful ...
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1100;Const intinf=1e9+Ten;intn,q;CharS[MAXN];intL,r;ll ANS[MAXN][MAXN];structpalintree{intch[maxn][ -],F[MAXN]; intN,tot,last; intLEN[MAXN],CNT[MAXN]; intS[MAXN]; intNewNodeintl) {MS0 (Ch[tot]); Cnt[tot]=0; len[tot]=l; returntot++; } voidinit () {tot=0; NewNode (0); NewNode (-1); Last=0; n=0; S[n]=-1; f[0]=1; } intGet_fail (intx) { while(s[n-len[x]-1]!=s[n]) x=F[x]; returnx; } voidAddintc) {c-='a'; s[++n]=C; Last=Get_fail (last); if(!Ch[last][c]) { intCur=newnode (len[last]+2); F[cur]=Ch[get_fail (F[last])][c]; CH[LAST][C]=cur; } Last=Ch[last][c]; Cnt[last]++; }}; Palintree pt;voidInit () {intLen=strlen (s+1); REP (L,1, Len) {Pt.init (); REP (R,l,len) {Pt.add (s[r]); ANS[L][R]=pt.tot-2; } }}intMain () {Freopen ("In.txt","R", stdin); while(~SCANF ("%d",&N)) {scanf ("%s", s+1); Init (); scanf ("%d",&q); while(q--) {scanf ("%d%d",&l,&R); printf ("%i64d\n", Ans[l][r]); } } return 0;}
View Code
This problem with a palindrome tree to do, but also a water problem ah water problem ....
hdu5658 CA Loves palindromic palindrome tree