CF 835D Palindromic characteristics(DP)

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 835D Palindromic characteristics(DP)相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/problemset/problem/835/D

题目:

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:

  1. Its left half equals to its right half.
  2. 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.

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.

题意:k阶回文串为其左右一半都为k-1的回文串基础上,然后自己也是回文串,比如bb为2阶回文串。

题解:dp[i][j]记录i—>j为几阶回文串,dp[i][j]从dp[i+1][j-1]的基础上转移过来,因为要左右都为k-1回文串的基础上,所以取值得时候从中间+1。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 
 9 typedef long long LL;
10 const int N=5500;
11 char s[N];
12 int ans[N],dp[N][N];
13 
14 int main(){
15     scanf("%s",s+1);
16     int n=strlen(s+1);
17     for(int i=1;i<=n;i++){
18         dp[i][i]=1;ans[1]++;
19         if(i<n&&s[i]==s[i+1]){
20             dp[i][i+1]=2;
21             ans[1]++;ans[2]++;
22         }
23     }
24     for(int k=3;k<=n;k++){
25         for(int i=1;i+k-1<=n;i++){
26             if(dp[i+1][i+k-2]&&s[i]==s[i+k-1]){ 
27                 dp[i][i+k-1]=dp[i][i+k/2-1]+1;
28                 for(int j=1;j<=dp[i][i+k-1];j++) ans[j]++;
29             }
30         }
31     }
32     for(int i=1;i<=n;i++) printf("%d ",ans[i]);
33     return 0;
34 }

 

以上是关于CF 835D Palindromic characteristics(DP)的主要内容,如果未能解决你的问题,请参考以下文章

[加强版] Codeforces 835D Palindromic characteristics (回文自动机DP)

CF1059B Forgery

cf/628/C

CF540C Ice Cave

[CF797C] Minimal string - 贪心,栈

CF1105D Kilani and the Game