Codeforces835D Palindromic characteristics

来源:互联网 发布:whisper是什么软件 编辑:程序博客网 时间:2024/06/11 21:33
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.

—————————————————————————————————————
题目的意思是给出一个字符串,求各个k回文的个数。k回文的定义,本身是回文且左一半是k-1回文
思路:区间dp,dp[i][j]表示区间[i,j]的回文等级,具体看代码
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define mod 10000007#define mem(a,b) memset(a,b,sizeof a)int dp[5005][5005];char s[5005];int ans[50005];int main(){    while(~scanf("%s",s))    {        int n=strlen(s);        mem(dp,0);        mem(ans,0);        for(int i=0; i<n; i++)        {            ans[1]++;            dp[i][i]=1;        }        for(int i=1; i<n; i++)        {            if(s[i-1]==s[i])                dp[i-1][i]=2,ans[1]++,ans[2]++;        }       for(int j=2; j<n; j++)            for(int i=0; i<n; i++)            {                if(i+j>=n)                    break;                if(s[i]==s[i+j]&&dp[i+1][i+j-1])                {                    dp[i][i+j]=dp[i][i+(j+1)/2-1]+1;                    for(int q=1; q<=dp[i][i+j]; q++)                    {                        ans[q]++;                    }                }            }        for(int i=1; i<n; i++)            printf("%d ",ans[i]);            printf("%d\n",ans[n]);    }    return 0;}


原创粉丝点击