• CodeForces 163A Substring and Subsequence


    题意:求a串的子串与b串的子序列有多少个相匹配,mod=1e9+7;

    思路:类似于LCS,如果会LCS的话就很容易(但是我没想通LCS的方程是怎么的来的)

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=5005;
    const int MOD=1e9+7;
    char a[maxn],b[maxn];
    int dp[maxn][maxn];
    
    int main()
    {
        while(~scanf("%s%s",a+1,b+1)){
            int lena=strlen(a+1);
            int lenb=strlen(b+1);
            memset(dp,0,sizeof(dp));
            long long ans=0;
            for(int i=1;i<=lena;i++){
                for(int j=1;j<=lenb;j++){
                    if(a[i]==b[j])dp[i][j]=(dp[i][j]+dp[i-1][j-1]+1)%MOD;
                    dp[i][j]=(dp[i][j]+dp[i][j-1])%MOD;
                }
                ans=(ans+dp[i][lenb])%MOD;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    py 5.11
    py 5.10
    py 5.9
    py 5.8
    python 5.7
    python 5.4
    python 5.3
    python 5.2
    python 4.28
    python 4.27
  • 原文地址:https://www.cnblogs.com/lalalatianlalu/p/8451862.html
Copyright © 2020-2023  润新知