Codeforces 835D

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


区间DP:以d[l][r]记录ch[l]ch[r]的子串的回文阶数。如果ch[lr]回文,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;
}
posted @ 2017-08-03 20:08  />.<\  阅读(183)  评论(0编辑  收藏  举报