HDU 4632 Palindrome subsequence (区间DP)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4632 Palindrome subsequence (区间DP)相关的知识,希望对你有一定的参考价值。

题意:给定一个字符串,问它的集合中有多少个回文串。

析:dp[i][j] 表示区间 i 到 j,有多少个回文串,

如果 s[i] == s[j]  dp[i][j] = dp[i+1][j] + dp[i][j-1] + 1。

否则 dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 50;
const int maxm = 1e6 + 5;
const int mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

int dp[maxn][maxn];
char s[maxn];

int dfs(int i, int j){
  if(i > j)  return 0;
  int &ans = dp[i][j];
  if(ans)  return ans;
  if(s[i] == s[j])  ans = dfs(i+1, j) + dfs(i, j-1) + 1;
  else ans = dfs(i+1, j) + dfs(i, j-1) - dfs(i+1, j-1);
  return ans = (ans + mod) % mod;
}

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    scanf("%s", s);
    ms(dp, 0);
    for(n = 0; s[n]; ++n)  dp[n][n] = 1;
    printf("Case %d: %d\n", kase, dfs(0, n - 1));
  }
  return 0;
}

  

以上是关于HDU 4632 Palindrome subsequence (区间DP)的主要内容,如果未能解决你的问题,请参考以下文章

hdu-4632 Palindrome subsequence (回文子序列计数)

hdu4632 Palindrome subsequence 回文子序列个数 区间dp

HDU 4632 Palindrome subsequence(区间DP求回文子序列数)

HDU 4632 Palindrome subsequence (区间DP)

Palindrome subsequence (区间DP)

hdu4632 回文子序列