Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
- Its left half equals to its right half.
- Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string tdivided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters.
Print |s| integers — palindromic characteristics of string s.
abba
6 1 0 0
abacaba
12 4 1 0 0 0 0
In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here.
题目大意:
一个回文串称作一级回文串,一个2级回文串其左半边和右半边相等且都是1级回文串。
问各个级别的回文串的个数。
思路:
直接区间Dp一下就行了。
设定Dp【i】【j】=x表示字符串【i,j】是x级回文串。
那么有:Dp【i】【j】=Dp【i+1】【j-1】+1;
这里需要保证字符串【i+1,j-1】是回文串;
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
bool Is[5005][5005];
ll dp[5005][5005];
ll ans[5005];
char a[5005];
int main()
{
while(~scanf("%s",a+1))
{
int n=strlen(a+1);
for(int len=0;len<=n;len++)
{
for(int i=1;i<=n;i++)
{
if(len==0)
{
dp[i][i]=1;Is[i][i]=true;
continue;
}
int j=i+len;
if(j<=n)
{
if(a[i]==a[j])
{
if(j-i+1==2||Is[i+1][j-1]==true)
{
int mid=(i+j)/2;
if((j-i+1)%2==1)mid--;
Is[i][j]=true;
dp[i][j]=dp[i][mid]+1;
}
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
ans[dp[i][j]]++;
}
}
for(int i=n-1;i>=1;i--)ans[i]+=ans[i+1];
for(int i=1;i<=n;i++)
{
printf("%I64d ",ans[i]);
}
printf("\n");
}
}