Senior's String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 355 Accepted Submission(s): 133
Problem Description
Xuejiejie loves strings most. In order to win the favor of her, a young man has two strings
X
,
Y
to Xuejiejie. Xuejiejie has never seen such beautiful strings! These days, she is very happy. But Xuejiejie is missish so much, in order to cover up her happiness, she asks the young man a question. In face of Xuejiejie, the young man is flustered. So he asks you for help.
The question is that :
Define the L as the length of the longest common subsequence of X and Y .( The subsequence does not need to be continuous
in the string, and a string of length L has 2L subsequences containing the empty string ). Now Xuejiejie comes up with all subsequences of length L of string X , she wants to know the number of subsequences which is also the subsequence of string Y .
The question is that :
Define the L as the length of the longest common subsequence of X and Y .( The subsequence does not need to be continuous
in the string, and a string of length L has 2L subsequences containing the empty string ). Now Xuejiejie comes up with all subsequences of length L of string X , she wants to know the number of subsequences which is also the subsequence of string Y .
In the first line there is an integer
T
, indicates the number of test cases.
In each case:
The first line contains string X , a non-empty string consists of lowercase English letters.
The second line contains string Y , a non-empty string consists of lowercase English letters.
1≤|X|,|Y|≤1000 , |X| means the length of X .
In each case:
The first line contains string X , a non-empty string consists of lowercase English letters.
The second line contains string Y , a non-empty string consists of lowercase English letters.
1≤|X|,|Y|≤1000 , |X| means the length of X .
For each test case, output one integer which means the number of subsequences of length
L
of
X
which also is the subsequence of string
Y
modulo
109+7
.
2 a b aa ab
1 2
题目分析:很好的一道题,二次dp,第一次求LCS,这个不多说,第二次记cnt[i][j]为到x的i位置和y的j位置时长度为dp[i][j]的公共子串的个数。因为是从x中选,那么就考虑x的第i个字符选不选
如果dp[i][j] == dp[i - 1][j]即长度相等,则不选cnt[i][j] += cnt[i][j - 1]
选的情况下,x的第i个字符必然和y的某个字符相等,那肯定选y中在j之前最后一个与之相等的
dp[i][j] == dp[i - 1][p - 1] + 1
cnt[i][j] += cnt[i - 1][p - 1]
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MOD = 1e9 + 7;
int const MAX = 1005;
char x[MAX], y[MAX];
int dp[MAX][MAX], cnt[MAX][MAX];
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(cnt, 0, sizeof(cnt));
scanf("%s %s", x + 1, y + 1);
int l1 = strlen(x + 1);
int l2 = strlen(y + 1);
for(int i = 1; i <= l1; i++)
for(int j = 1; j <= l2; j++)
dp[i][j] = (x[i] == y[j] ? dp[i - 1][j - 1] + 1 : max(dp[i - 1][j], dp[i][j - 1]));
cnt[0][0] = 1;
for(int i = 1; i <= l1; i++)
cnt[i][0] = 1;
for(int j = 1; j <= l2; j++)
cnt[0][j] = 1;
for(int i = 1; i <= l1; i++)
{
int p = 0;
for(int j = 1; j <= l2; j++)
{
if(x[i] == y[j])
p = j;
if(dp[i][j] == dp[i - 1][j])
cnt[i][j] = (cnt[i][j] % MOD + cnt[i - 1][j] % MOD) % MOD;
if(p && dp[i][j] == dp[i - 1][p - 1] + 1)
cnt[i][j] = (cnt[i][j] % MOD + cnt[i - 1][p - 1] % MOD) % MOD;
}
}
printf("%d\n", cnt[l1][l2] % MOD);
}
}

本文深入探讨了如何通过计算两个字符串的最长公共子序列(LCS),进一步筛选出这些子序列中同时也是目标字符串子序列的数量。通过二次动态规划方法,实现高效的子序列匹配计算。

357

被折叠的 条评论
为什么被折叠?



