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]);
}