CodeForces - 835D Palindromic characteristics (dp)

題目鏈接:http://codeforces.com/problemset/problem/835/D點擊打開鏈接

D. Palindromic characteristics
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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:

  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.


感覺描述有點不清楚啊。。

  1. Its left half equals to its right half.這個東西我總是理解成左右得相等。。

主要的想法就是從低階往高階dp
dp【i】【j】爲s【i~j】最高階迴文串
然後外層枚舉串長度
內層從左到右
判定是否成立 注意判定子條件是ij必須相差2以上

#include <bits/stdc++.h>
using namespace std;
int dp[5555][5555];
int main()
{
    string s;
    cin >> s;
    int len=s.length();

    int ans[len+1];
    for(int i=0;i<=len;i++)
        ans[i]=0;
    for(int i=0;i<len;i++)
    {
        dp[i][i]=1;
        ans[1]++;
    }
    for(int j=2;j<=len;j++)
        for(int i=0;i+j-1<=len;i++)
        {
            int r=i+j-1;
                if((dp[i+1][r-1]==0&&i+1<=r-1)||s[i]!=s[r])
                    dp[i][r]=0;
                else
                    dp[i][r]=dp[i][i+j/2-1]+1;
            if(dp[i][r])
                ans[dp[i][r]]++;
        }
    for(int i=len-1;i>=1;i--)
        ans[i]+=ans[i+1];
    for(int i=1;i<=len;i++)
        cout << ans[i] <<" ";
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章