码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #427 (Div. 2) D dp

时间:2017-08-06 14:17:09      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:ber   when   logs   class   form   round   turn   second   hat   

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-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] = 0, dp[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|).

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5005;

int dp[N][N], ans[N];
char s[N];
int main()
{
    scanf("%s", s+1);
    int n = strlen(s+1);
    rep(len,1,n) for(int i=1; i+len-1<=n; ++i)
    {
        int j = i+len-1;
        if(len==1) dp[i][j]=1;
        else if(len==2) dp[i][j]= s[i]==s[j] ? 2 : 0;
        else if(dp[i+1][j-1]==0 || s[i]!=s[j]) dp[i][j]=0;
        else dp[i][j]=dp[i][i+len/2-1]+1;
        ++ans[dp[i][j]];
    }
    per(i,n-1,1) ans[i] += ans[i+1];
    rep(i,1,n) printf("%d ", ans[i]);
    puts("");

    return 0;
}

Codeforces Round #427 (Div. 2) D dp

标签:ber   when   logs   class   form   round   turn   second   hat   

原文地址:http://www.cnblogs.com/sbfhy/p/7274250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!