Palindrome subsequence HDU-4632 interval dp palindrome problem winter vacation training

In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence <A, B, D> is a subsequence of <A, B, C, D, E, F>.
(http://en.wikipedia.org/wiki/Subsequence)

Given a string S, your task is to find out how many different subsequence of S is palindrome. Note that for any two subsequence X = <Sx1, Sx2, …, Sxk> and Y = <Sy1, Sy2, …, Syk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if Sxi = Syi. Also two subsequences with different length should be considered different.
Input
The first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.
Output
For each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.
Sample Input
4
A
AAAAA
goodafternooneveryone
welcometoooxxourproblems
the Sample the Output
Case 1: 1
Case 2: 31 is
Case. 3: 421
Case. 4: 960
with dp recording 1 to len the number of palindromic subsequences have, we grew interval to push the large range, so with the front and rear When the same character appears, there is dp[j][k]=max(dp[j][k],dp[j][k-1]+dp[j+1][k]+1)%10007, because the middle can be combined with the first or the last.
When it is different before and after, yes dp[j][k]=(dp[j][k-1]+dp[j+1][k]-dp[j+1][k-1]+10007)%10007, you can combine the first with the first + and the latter. Length-the length that appears repeatedly in the middle. Note that the modulus must be added at the end in the process of +mod, so as to ensure that there will be no negative modulus and some special cases

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
using namespace std;
const int p=1e4+7;
const int mod=10007;
const int maxn=1500;
typedef long long ll;
const int inf=0x3f3f3f3f;   
int dp[maxn][maxn];
char s[maxn];
ll mode(ll a,ll b){
    
    
    ll sum=1;
    a=a;
    while(b>0){
    
    
        if(b%2==1)
            sum=(sum*a);
            b/=2;
            a=(a*a);
    }
    return sum;
}
void solve(){
    
    
    int t;
    cin>>t;
    int cnt=0;
    while(t--){
    
    
        scanf("%s",s+1);
        memset(dp,0,sizeof(dp));
        int len=strlen(s+1);
        for(int i=1;i<=len;i++)
            dp[i][i]=1;
        for(int i=1;i<=len;i++){
    
    //枚举字符串长度
            for(int j=1;j+i<=len+1;j++){
    
    //枚举端点
                int k=i+j-1;//
                if(s[j]==s[k])
                    dp[j][k]=max(dp[j][k],dp[j][k-1]+dp[j+1][k]+1)%10007;
                else
                    dp[j][k]=(dp[j][k-1]+dp[j+1][k]-dp[j+1][k-1]+10007)%10007;
            }   
        }
        printf("Case %d: %d\n",++cnt,dp[1][len]);
    }
}
int main(){
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    system("pause");    
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/112741291