Codeforces 835D Round #427 D-Palindromic characteristics—— DP+回文串

58 篇文章 1 订阅
19 篇文章 0 订阅

注意:凡是可以用O(n^2)算法求回文串时候,都可以用dp求解。dp[i][j] = (s[i] == s[j]) ? dp[i + 1][j -1] : 0(即当前两端是否相等,若相等,则只取决于中间是不是回文串)。预处理的时候,只需要令所有的dp[i][i] = 1,dp[i][i - 1] = 1(方便转移)即可。

题解:

Observation I.

If the string is k-palindrome, then it is (k - 1)-palindrome.

Observation II.

The string is k-palindrome if and only if the both following conditions are true:

  1. It is a palindrome.
  2. It's left half is non-empty (k - 1)-palindrome.

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-palindrome.
  • The dynamics is calculated in the order of non-decreasing of substring lengths.
  • The values for l = r and l = r - 1 are computed trivially.
  • Let r - l > 1. Then, if s[l] ≠ s[r] or dp[l + 1][r - 1] = 0dp[l][r] = 0. Otherwise dp[l][r] = dp[l][m] + 1, where .

When we have dp values, we can calculate cnt[k] — the number of substrings, which dp value is k. Then the number of substrings that are k-palindromes is .

The solution works in O(|s|2) time and uses O(|s|2) memory.

Also, you could notice that the string can be no more than -palindrome, and solve the problem in  time, reducing the memory usage to O(|s|).

Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 10;
int dp[maxn][maxn];
char s[maxn];
int cnt[maxn];
int lens;
int main()
{
	scanf("%s",s);lens = strlen(s);
	for(int i = 0;i < lens;i++){
		dp[i][i] = 1;dp[i][i-1] = 1;
	}
	cnt[1] += lens;
	for(int len = 2;len <= lens;len++){
		for(int i = 0;i < lens;i++){
			int j = i + len - 1;
			if(j >= lens) break;
			if(s[i] != s[j] || dp[i + 1][j - 1] == 0){
				dp[i][j] = 0;
			else 
				dp[i][j] = dp[i][i + (len / 2) - 1] + 1;
			cnt[dp[i][j]]++;
		}
	}
	for(int i = lens;i >= 1;i--){
		cnt[i] += cnt[i + 1];
	}
	for(int i = 1;i <= lens;i++){
		printf("%d ",cnt[i]);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值