HDU 5282 Senior's String (两次dp LCS预处理)

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


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 .
 
Input
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 .
 
Output
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 .
 
Sample Input
  
2 a b aa ab
 
Sample Output
  
1 2
题目大意:给两个字符串x,y,从字符串x中取出所有LCS(x,y)长度的子串,问有多少个也是y的子串

题目分析:很好的一道题,二次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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值