Codeforces Round #427 (Div. 2) D. Palindromic characteristics

D. Palindromic characteristics

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 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-len阶回文串,k阶回文串的左边是有k-1阶回文串组成的,1阶回文串就是字符串的每一个子串回文串。

用dp来做,dp[i][j]表示字符串str[i...j]是否是回文串。O(n^2)的复杂度。

然后求dp[i][j]是第几阶回文串,可以用递归来做,如果str[i..j]不是回文串的话,则返回0,否则返回str[i...m]+1,m表示子串str[i....j]的左串的右边位置。i==j,在递归会出现m<l的因为,返回0;

只好可以把1阶回文串的左边看成由0阶回文串组成的。由于k阶回文串又是k-1,k-2...1阶会文串,所以要ans[i-1] += ans[i]

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAX = 5010;
 4 char str[MAX];
 5 int dp[MAX][MAX], ans[MAX], n;
 6 int getK(int l, int r) {
 7     if(dp[l][r] == 0) return 0;
 8     else {
 9         int m = (l+r);
10         if(m&1) return getK(l,m/2)+1;
11         else return getK(l,m/2-1)+1;
12     }
13 }
14 int main() {
15     scanf("%s",str+1);
16     int n = strlen(str+1);
17     for(int i = 1; i <= n; i ++) {
18         dp[i][i] = 1;
19         if(i+1 <= n && str[i] == str[i+1]) dp[i][i+1] = 1;
20     }
21     for(int i = 3; i <= n; i ++) {
22         for(int j = 1; j <= n-i+1; j ++) {
23             int r = j+i-1;
24             if(dp[j+1][r-1] && str[j] == str[r]) dp[j][r] = 1;
25         }
26     }
27     for(int i = 1; i <= n; i ++) {
28         for(int j = i; j <= n; j ++) {
29             ans[getK(i,j)] ++;
30         }
31     }
32     for(int i = n; i > 0; i --)
33         ans[i-1] += ans[i];
34     for(int i = 1; i <= n; i ++)
35         printf("%d%c",ans[i],(i==n?‘\n‘:‘ ‘));
36     return 0;
37 }
时间: 2024-10-09 20:16:10

Codeforces Round #427 (Div. 2) D. Palindromic characteristics的相关文章

Codeforces Round #427 (Div. 2) D. Palindromic characteristics(Manacher求回文串)

题目链接:Codeforces Round #427 (Div. 2) D. Palindromic characteristics 题意: 给你一个串,定义k-th回文串,让你求每个k-th的数量. 题解: manacher处理好后做一下dp就行了. 当然也可以直接dp不用manacher. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 cons

Codeforces Round #427 (Div. 2) C. Star sky(二维前缀和)

题目链接:Codeforces Round #427 (Div. 2) C. Star sky 题意: 在一个二维平面上有n个星星,每个星星有一个初始的亮度,每过去一秒,星星的亮度变化为(s+1)%(c+1). 现在有q个询问,问t秒后一个矩形区域的星星的总亮度为多少. 题解: 由于c不大,将每颗星星按照初始亮点分类,每一类在任意时间的亮度都是相同的,然后对应加一加就行了. 1 #include<bits/stdc++.h> 2 #define RT(l,r) (l+r|l!=r) 3 #de

Codeforces Round #427 (Div. 2) D dp

D. Palindromic characteristics 题意:求给定字符串每阶回文子串有多少个. tags:根本没想到 dp..直接看官方题解吧 dp[i][j] 代表第 i 个字符到第 j 个字符的子串是几阶回文. Solution. Let's calculate the following dp. dp[l][r] is the maximum k such that the substring built from characters from l to r is k-palin

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call some square matrix with integer values in its cells palind

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

Codeforces Round #427 (Div. 2)

B. The number on the board 题意: 有一个数字,它的每个数位上的数字的和不小于等于k.现在他改变了若干位,变成了一个新的数n,问现在的数和原来的数最多有多少位不同. 思路: 如果现在的数字各位数字之和大于等于k,那么它就可能没有被改变. 反之,那么每个数的最大改变量就是9减去这个数,于是把9减去每个数得到的结果从大到小排序,用k减去现在的数位和得到的结果记为kk,遍历一遍差量数组,用kk去减,知道kk小于0跳出. 代码: 1 #include <stdio.h> 2

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #580 (Div. 1)

Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了,放个代码吧. #include<bits/stdc++.h> using namespace std; void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-'