String Game Gym - 102800C(动规)

String Game
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Clair and Bob play a game. Clair writes a string of lowercase characters, in which Bob sets the puzzle
by selecting one of his favorite subsequence as the key word of the game. But Bob was so stupid that he
might get some letters wrong.
Now Clair wants to know whether Bob’s string is a subsequence of Clair’s string and how many times
does Bob’s string appear as a subsequence in Clair’s string. As the answer may be very large, you should
output the answer modulo 109 + 7.
Input
The first line is Clair’s string (whose length is no more than 5000), and the second line is Bob’s string
(whose length is no more than 1000).
Output
Output one line, including a single integer representing how many times Bob’s string appears as a
subsequence in Clair’s string. The answer should modulo 109 + 7.
Examples

输入:
eeettt
et
输出:
9
输入:
eeettt
te
输出:
0

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
char a[5005], b[1005];
int dp[1005][5005];   //第一位存已经匹配到的数位,第二位存当前进行到的数位
int n, m, i, j;
int main() 
{
    scanf("%s%s", a + 1, b + 1);
    memset(dp, 0, sizeof(dp));
    n = strlen(a + 1);
    m = strlen(b + 1);
    for (i = 1; i <= m; i++) 
    {
        for (j = 1; j <= n; j++) 
        {
            if (a[j] == b[i]) 
            {
                if (i == 1)
                    dp[i][j] = (1 + dp[i][j - 1]) % M;
                else
                    dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % M;
            } 
            else 
            {
                dp[i][j] = dp[i][j - 1];
            }
        }
    }
    printf("%d\n", dp[m][n]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值