Codeforces Round #427 (Div. 2) D dp

Posted 掉血菜鸡煮熟中

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #427 (Div. 2) D dp相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #427 (Div. 2) D. Palindromic characteristics

Codeforces Round #427 C

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

codeforces round #427 div2

Codeforces Round #427 (Div. 2) D dp

Codeforces Round #427 (Div. 2)