pid=5282">http://acm.hdu.edu.cn/showproblem.php?pid=5282

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
/**
hdu5282 最长公共子序列的变形
题目大意:给定两个字符串。求二者的最长公共子序列,在a中出现过的。有多少是b的子序列
解题思路:来自官方题解。
首先我们用O(n2)的动态规划算法处理出dp数组,dp[i][j]表示X串的前i个字符和Y
串的前j个字符的最长公共子序列的长度,在这个基础上我们再进行一个动态规划。
用f[i][j]表示在X串的前i个字符中。有多少个长度为dp[i][j]的子序列在Y的前j个
字符中也出现了。转移:若dp[i−1][j]==dp[i][j],则f[i][j]+=f[i−1][j]。表示i
这个字符不选;再考虑选i这个字符。找到Y串前j个字符中最靠后的与X[i]匹配的字
符的位置,设为p,若dp[i−1][p−1]+1==dp[i][j],则f[i][j]+=f[i−1][p−1]。终于
的答案即为f[n][m]。 复杂度O(n2)。 */
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const int maxn=1005;
int dp[maxn][maxn],n,m,wei[maxn][maxn];
char a[maxn],b[maxn];
LL f[maxn][maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",a,b);
n=strlen(a);
m=strlen(b);
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
if(a[i]==b[j])
dp[i+1][j+1]=max(dp[i][j]+1,dp[i+1][j+1]);
}
}
memset(wei,0,sizeof(wei));
for(int i=1;i<=m;i++)
{
for(int j=0;j<26;j++)
{
wei[i][j]=wei[i-1][j];
}
wei[i][b[i-1]-'a']=i;
}
memset(f,0,sizeof(f));
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(dp[i][j]==0)
{
f[i][j]=1;
continue;
}
if(dp[i-1][j]==dp[i][j])
{
f[i][j]=(f[i][j]+f[i-1][j])%mod;
}
int p=wei[j][a[i-1]-'a'];
if(p)
{
if(dp[i-1][p-1]+1==dp[i][j])
{
f[i][j]=(f[i][j]+f[i-1][p-1])%mod;
}
}
}
}
printf("%I64d\n",f[n][m]);
}
return 0;
}

hdu5282 最长公共子序列的变形的更多相关文章

  1. DP专辑之最长公共子序列及其变形

    vijos1111(裸的最长公共子序列) 链接:www.vijos.org/p/1111 题解:好久没有写最长公共子序列了,这题就当是复习了.求出最长公共子序列,然后用两个单词的总长度减去最长公共子序 ...

  2. HDU 1080 Human Gene Functions - 最长公共子序列(变形)

    传送门 题目大意: 将两个字符串对齐(只包含ACGT,可以用'-'占位),按照对齐分数表(参见题目)来计算最后的分数之和,输出最大的和. 例如:AGTGATG 和 GTTAG ,对齐后就是(为了表达对 ...

  3. hdu1503 最长公共子序列变形

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...

  4. hdu 1080 dp(最长公共子序列变形)

    题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G -  G ...

  5. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  6. hdu1243(最长公共子序列变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...

  7. 51Nod 1092 回文字符串 | 最长公共子序列变形

    求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...

  8. poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53414   Accepted: 18449 Desc ...

  9. poj 1080 Human Gene Functions (最长公共子序列变形)

    题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...

随机推荐

  1. Redis windows版本的启停bat脚本命令

    Reids windows版本安装 redis windows官网推荐:https://github.com/MicrosoftArchive/redis/releases 下载解压即可. 启停bat ...

  2. POJ 2154

    这题的时间卡的.... 必须用欧拉来优化,而且要加素数表.最重要是,因为最后结果要/n,而数据很大,所以,必须在之前就先/n了,否则会爆数据. #include <iostream> #i ...

  3. jQuery Mobile(jqm)button的隐藏和显示,包含a标签,圆角和非圆角button

    在移动互联网时代,HTML5开发越来越收到欢迎. 于是各种HTML5的框架都出来了.因为对于jquery的熟悉,jquery mobile 为多数人选择学习的对象.我也是众多追求者之中的一个.近期一直 ...

  4. simple_strtoul()分析

    此函数有以下几点值得注意:1.第一个参数中的const.一般在函数的形参中,如果我们只是希望调用者使用该参数,而不会去改变该参数内容(一般是指针指向的内容),则可以声明为const.2.第二个参数.C ...

  5. 91.Bower : ENOGIT git is not installed or not in the PATH 解决方法

    转自:https://www.haorooms.com/post/bower_error 今天在用bower安装依赖的时候,出现了Bower : ENOGIT git is not installed ...

  6. 日常问题记录-- java.lang.IllegalArgumentException: taglib definition not consistent with specification version

    转自:https://www.cnblogs.com/carterzhang/p/4288650.html 背景: tomcat8.0中使用taglib 错误表现: java.lang.Illegal ...

  7. BZOJ 1898 构造矩阵+矩阵快速幂

    思路: T的最小公倍数是12 那么12以内暴力 整除12 的部分用矩阵快速幂 //By SiriusRen #include <cstdio> #include <cstring&g ...

  8. @JsonIgnore忽略属性,返回的json中不包含字段

    @JsonIgnore的使用: 实体类中加@JsonIgnore注解 package com.baidu.entity; import com.fasterxml.jackson.annotation ...

  9. java高级——生产者消费者问题

    多线程是一个很重要的应用,本节讲述多线程中同步问题 public class ThreadDemo { public static void main(String[] args) { Resourc ...

  10. 把阅读平台从Acrobat转到Endnote

    现在阶段的学习完全到了自学,那么整理文献也必须有自己的一套思路和办法.不像硕士阶段导师给论文,而是自我指导读论文,必须有一种类似版本控制和库的快速调用的机制.而Endnote越来越必须了. 首先遇到的 ...