Gym - 101628A DP

題意:

在a字符串中刪除某個w子串(可以分散的),問能到多少種b字符串

樣例:

Input
weewrshkim
sim
Output
1
Input
qqqaaabbbcccfffrrr
qabcfr
Output
729

思路:

題目意思實則就是求a串中的w串有多少種組成種類。

dp[i][j] 表示 a串前i個字符 構成w字符串前j個字母的方案數。

代碼:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>
#include <string.h>
#include <string>
#include <queue>
#include <set>
#include <bits/stdc++.h>
using namespace std;

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]-'A'].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]-'A'].size()-1;j>=0;j--){
            cnt=id[str[i]-'A'][j];
            dp[cnt]+=dp[cnt-1];
            dp[cnt]%=MOD;
        }
    }
    cout<<dp[len]%MOD<<endl;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章