Cattle-off practice match 60 C. Operating Highlights

Link
meaning of problems:
seeking string \ (S \) the number of different sequences of
ideas:
setting \ (dp [i, j] \) is a front \ (I \) characters in length \ (J \) the number of different sequences is
not considered which are repeated: \ (F [I, J] = F [. 1-I, J] + F [. 1-I, J-. 1] \)
consider subtracting repeated subsequences: can be found if the \ (I \) is present before \ (s [i] \) of this character, provided this position is the \ (T \) , a length \ (J \) to \ (T \) end of the sequence in \ (f [i, j] \) was calculated twice, the repeated portion is \ (f [t-1]
[j-1] \) at recording thus \ (I \) before the first \ (s [i] \) where they appear, can be repeated portion is subtracted
Note: this question of length \ (0 \) is valid sequence
codes:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> pii;
 
const ll mod=1e9+7;
const int N=1010;
char s[N];
ll dp[N][N];
int pre[30];
int main() {
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    cin>>s+1;
    dp[0][0]=1;
    for(int i=1;i<=n;i++) {
        dp[i][0]=1;
        for(int j=1;j<=min(i,k);j++) {
            dp[i][j]=(dp[i-1][j]+dp[i-1][j-1])%mod;
            if(pre[s[i]-'a']) dp[i][j]=(dp[i][j]+mod-dp[pre[s[i]-'a']-1][j-1])%mod;
        }
        pre[s[i]-'a']=i;
    }
    cout<<dp[n][k]<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/c4Lnn/p/12596110.html