Codeforces 427Div2 D、835D Palindromic characteristics

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~|s|级回文串各有几个,对于k级回文串的定义:如果一个长度字符串是k级字符串,则前一半的字符串是k - 1级回文串,后一半也是k - 1 级回文串。

    思路: DP[l][r]表示从l~r之间是DP[l][r]级回文串,当满足str[l] == str[r] && DP[l + 1][r - 1] != 0 时 DP[l][r] = DP[l][r - (r - l)/2 - 1] + 1,否则DP[l][r] = 0。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define MAX 5005
using namespace std;
int DP[MAX][MAX];

int main( )
{
	char str[MAX];
	cin >> str;
	int len = strlen(str);
	for(int j = 0; j < len; j++)
		if(str[j] == str[j + 1]) DP[j][j + 1] = 2,DP[j][j] = 1;
		else DP[j][j] = 1;
	int dif;
	for(int j = 2; j < len; j++){
		for(int k = j - 2; k >= 0; k--){
			if(str[j] == str[k] && DP[k + 1][j - 1] != 0){
				dif = (j - k)/2 + 1;
				DP[k][j] = DP[k][j - dif] + 1;
			}
			//原来写法: 
			/*if(str[j] == str[k]){
				dif = (j - k)/2 + 1;
				if(DP[k][j - dif] != 0 && DP[k + dif][j] != 0) DP[k][j] = DP[k][j - dif] + 1;
				else if(DP[k + 1][j - 1] != 0) DP[k][j] = 1;
			}*/
			//hack date: abaaca
		}
	}
/*	for(int j = 0; j < len; j++){
		for(int k = 0; k < len; k++)
			cout << DP[j][k] << " ";
		cout << endl;
	}*/
	int ans[MAX] = {0};
	for(int j = 0; j < len; j++)
		for(int k = j; k < len; k++)
			while(DP[j][k] > 0) ans[DP[j][k]--]++;
	for(int j = 1; j <= len; j++)
		cout << ans[j] << " ";
	cout << endl;
}
        在晚上打的时候都WA6,在补题中看了眼题解的后马上就明白了。但是,也不明白自己原来是怎么WA的,最后还是想出了一组数据成功证明我原来写的是错的。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值