gym101628 problemA Arthur's Language 递推DP

http://codeforces.com/gym/101628/problem/A

题意 给出两个串 a b
问有多少种方法使得 a删除一些字母后变成b串

解题思路:
一开始以为是组合数取模啥的,后来想一想,可以用DP写 因为每一个字母的状态只与他前一个字母的状态有关,这样的话就很好写了。
dp[i][j] 表示 前i个字符 构成b字符串前j个字母的方案数。
后来想一想,第一维其实是可以省掉的, 相当于滚动数组。

#include<iostream>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<map>
#include<string>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1
using namespace std;
vector<int> V[10];
const int MAX=1e5+10;
string str;
char ch[14];
vector<int> id[222];
long long dp[11];
const int MOD=1e9+7;
int main(){
    cin>>str;
    scanf("%s",ch+1);
    int len=strlen(ch+1);
    memset(id,-1,sizeof id);
    for(int i=1;i<=len;i++){
        id[ch[i]].push_back(i);
    }
    dp[0]=1;
    int len2=str.length();
    str='0'+str;
    for(int i=1;i<=len2;i++){
        int cnt;
        for(int j=id[str[i]].size()-1;j>=0;j--){
            cnt=id[str[i]][j];
            dp[cnt]+=dp[cnt-1];
            dp[cnt]%=MOD;
        }
    }
    cout<<dp[len]%MOD<<endl;
}

猜你喜欢

转载自blog.csdn.net/lifelikes/article/details/78807670