Codeforces 835D

题意:给出一个字符串,字符串的每个字符是1阶回文,若字符串的某个长度为t的子串回文,这个子串的回文阶数是同起点开头长度为⌊t/2⌋的串的回文阶数加1。现在要求统计所有回文阶数对应包含的子串数量


区间DP:以d[l][r]记录ch[l]~ch[r]的子串的回文阶数。如果ch[l~r]回文,d[l][r] = d[l][(l+r-1)>>1]+1

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn = 5000 + 10;
const int INF = 0x3f3f3f3f;

char ch[maxn];
int d[maxn][maxn];
int res[maxn];

int main()
{
    memset(d, 0, sizeof(d));
    memset(res, 0, sizeof(res));
    scanf("%s", ch+1);
    int n = strlen(ch+1);

    for(int i = 1; i <= n; i++){
        d[i][i] = 1;
        res[1]++;
    }

    for(int i = 1; i <= n - 1; i++){
        if(ch[i] == ch[i+1]){
            d[i][i+1] = 2;
            res[2]++;
        }
    }

    for(int len = 3; len <= n; len++){
        for(int l = 1, r; (r = l + len - 1) <= n; l++){
            if(d[l+1][r-1] && ch[l] == ch[r]){
                d[l][r] = d[l][(l + r - 1) >> 1] + 1;
                res[d[l][r]]++;
            }
            else d[l][r] = 0;
        }
    }

    for(int i = n - 1; i > 0; i--)
        res[i] += res[i+1];

    for(int i = 1; i <= n; i++){
        printf("%d ",res[i]);
    }printf("\n");

    return 0;
}

转载于:https://www.cnblogs.com/shh00/p/7281653.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值