链接:
大意:
找一个字符串的substring子串与另一个字符串的subsequence子序列相等的个数
思路:DP
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
void solve()
{
string a, b;cin >> a >> b;
int n = a.size(), m = b.size();
//dp[i][j]是以i结尾的substring和以j结尾的subsequence
vector<vector<int>>dp(n + 1, vector<int>(m + 1));
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
dp[i][j] = dp[i][j - 1]; //默认是以i结尾的字串和前j-1个子序列的方案数
if (a[i - 1] == b[j - 1])
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] + 1) % mod; // 方案数为(i-1,j-1)加上一种相同的尾
}
ans = (ans + dp[i][m]) % mod;
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}