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


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632

    题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串)。
    解题思路:

    设dp[i][j]为[i,j]的回文子序列数,那么得到状态转移方程:
    dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+MOD)%MOD
    if(str[i]==str[j])
      dp[i][j]+=dp[i-1][j+1]+1

    代码:

     1 #include<cstdio>
     2 #include<cmath>
     3 #include<cctype>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<vector>
     8 #include<queue>
     9 #include<set>
    10 #include<map>
    11 #include<stack>
    12 #include<string>
    13 #define lc(a) (a<<1)
    14 #define rc(a) (a<<1|1)
    15 #define MID(a,b) ((a+b)>>1)
    16 #define fin(name)  freopen(name,"r",stdin)
    17 #define fout(name) freopen(name,"w",stdout)
    18 #define clr(arr,val) memset(arr,val,sizeof(arr))
    19 #define _for(i,start,end) for(int i=start;i<=end;i++)
    20 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
    21 using namespace std;
    22 typedef long long LL;
    23 const int N=1e3+5;
    24 const int MOD=10007;
    25 const int INF=0x3f3f3f3f;
    26 const double eps=1e-10;
    27 
    28 int dp[N][N];
    29 char str[N];
    30 
    31 int main(){
    32     int t,cas=0;
    33     cin>>t;
    34     while(t--){
    35         cin>>str;
    36         int n=strlen(str);
    37         memset(dp,0,sizeof(dp));
    38         for(int i=0;i<n;i++){
    39             dp[i][i]=1;
    40         }
    41         for(int len=1;len<n;len++){
    42             for(int i=0;i+len<n;i++){
    43                 int j=i+len;
    44                 dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+MOD)%MOD;
    45                 //如果str[i]==str[j],单独两个字符str[i],str[j]能组成一个回文串,同样也能跟[i-1,j+1]的所有回文串组成新的回文串
    46                 if(str[i]==str[j])
    47                     dp[i][j]+=dp[i+1][j-1]+1;
    48             }
    49         }
    50         cout<<"Case "<<++cas<<": "<<dp[0][n-1]%MOD<<endl;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    基于XMPP协议的Android即时通信系
    codeforces 830 B Cards Sorting
    [SCOI2007]修车
    spoj GSS4
    hdu 3652 B-number
    【模版】多项式乘法 FFT
    hdu 3642 Get The Treasury
    hdu 1255 覆盖的面积
    hdu 4553 约会安排
    UVA-11992 Fast Matrix Operations
  • 原文地址:https://www.cnblogs.com/fu3638/p/8860376.html
Copyright © 2020-2023  润新知