当前位置: 代码迷 >> 综合 >> HDU 4632 Palindrome subsequence(动态规划)
  详细解决方案

HDU 4632 Palindrome subsequence(动态规划)

热度:97   发布时间:2023-11-22 13:29:22.0

文章目录

  • 题目
  • AC代码


题目

本题链接:Palindrome subsequence

本博客给出本题截图
在这里插入图片描述

AC代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>using namespace std;const int N = 1010, mol = 10007;int f[N][N];int main()
{
    int T;cin >> T;int t = 1;while (T -- ){
    string s;cin >> s;int n = s.size();memset(f, 0, sizeof f);for (int i = 0; i < n; i ++ ) f[i][i] = 1;for (int len = 2; len <= n; len ++ )for (int i = 0; i + len - 1 < n; i ++ ){
    int j = i + len - 1;f[i][j] =  (f[i + 1][j] + f[i][j - 1] - f[i + 1][j - 1] + mol) % mol;if (s[i] == s[j]) f[i][j] = (f[i][j] + f[i + 1][j - 1] + 1 + mol) % mol;}printf("Case %d: %d\n", t ++, f[0][n - 1] % mol);}return 0;
}

  相关解决方案