D. Palindromic characteristics
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 t divided 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.
Input
The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters.
Output
Print |s| integers — palindromic characteristics of string s.
Examples
input
abba
output
6 1 0 0
input
abacaba
output
12 4 1 0 0 0 0
Note
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.
题意 : 统计 1 ~ nl 的回文串,字符串 s 是 k 阶回文串当且仅当 s 是回文串且左右字符串是 k - 1 阶回文串
思路 : 先算出 1 和 2,枚举其他长度 i 和左端点 l,满足 s 是回文串,且左右字符相同时 dp[i][l] = dp[i][i + l / 2 - 1] + 1;
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MAX = 5e3 + 10;
int dp[MAX][MAX],n[MAX];
char s[MAX];
void solve(int nl){
for(int i = 3; i <= nl; i++){
for(int l = 0; l + i - 1 < nl; l++){
int r = l + i - 1;
if(!dp[l + 1][r - 1] || s[l] != s[r]) continue;
dp[l][r] = dp[l][l + i / 2 - 1] + 1;
for(int j = dp[l][r]; j >= 1; j--) n[j]++;
}
}
for(int i = 1; i <= nl; i++) printf("%d ",n[i]);
}
int main()
{
scanf("%s",s);
int nl = strlen(s);
dp[0][0] = n[1] = 1;
for(int i = 1; i < nl; i++){
n[1] += dp[i][i] = 1;
if(s[i - 1] == s[i]) n[2]++,dp[i - 1][i] += 2,n[1]++;
}
solve(nl);
return 0;
}