Codeforces Round #427 (Div. 2) D. Palindromic characteristics(Manacher求回文串)
题目链接:Codeforces Round #427 (Div. 2) D. Palindromic characteristics
题意:
给你一个串,定义k-th回文串,让你求每个k-th的数量。
题解:
manacher处理好后做一下dp就行了。
当然也可以直接dp不用manacher.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 const int N=5000+7; 6 7 char s[N]; 8 int ans[N],n; 9 struct Manacher{ 10 char str[N<<1]; 11 int p[N<<1],len,mx,id,tl,ans,i; 12 int maxlen(char *s){ 13 len=strlen(s),mx=0,id=0,tl=0,str[tl++]='$',str[tl++]='#'; 14 for(i=0;i<len;i++)str[tl++]=s[i],str[tl++]='#'; 15 for(i=2,str[tl]=0,ans=0;i<tl;i++){ 16 p[i]=mx>i?min(p[(id<<1)-i],mx-i):1; 17 while(str[i-p[i]]==str[i+p[i]])p[i]++; 18 if(i+p[i]>mx)mx=i+p[i],id=i; 19 if(ans<p[i])ans=p[i]; 20 } 21 return ans-1; 22 } 23 }M; 24 25 int is[N][N]; 26 27 int main() 28 { 29 scanf("%s",s); 30 M.maxlen(s); 31 F(i,1,M.len)is[i][i]=1; 32 ans[1]=M.len; 33 F(i,1,M.len*2) 34 { 35 if(M.p[i]<2)continue; 36 int R; 37 R=M.p[i]/2; 38 if(M.str[i]!='#') 39 { 40 int idx=i/2; 41 for(int j=R-1;j>=1;j--) 42 { 43 int l=idx-j,r=idx-1; 44 is[l][idx+j]=is[l][r]+1; 45 ans[is[l][idx+j]]++; 46 } 47 }else 48 { 49 int idx=i/2; 50 for(int j=R;j>=1;j--) 51 { 52 int l=idx-j+1,r=idx; 53 is[l][idx+j]=is[l][r]+1; 54 ans[is[l][idx+j]]++; 55 } 56 } 57 } 58 for(int i=M.len-1;i;i--)ans[i]+=ans[i+1]; 59 F(i,1,M.len)printf("%d%c",ans[i]," \n"[i==M.len]); 60 return 0; 61 }