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. u-boot学习(六):自己写bootloader

    依照前面分析的u-boot的启动流程,自己写一个简单的Bootloader.这是參考韦东山老师的视频写的. 1.初始化硬件:关看门狗.设置时钟.设置SDRAM.初始化NAND Flash 2.假设Bo ...

  2. IOS假设将一个十六进制的color转换成UIColor,非常有用

    UI给开发的效果图非常多时候标注着十六进制的Color,而程序中用到的往往是UIColor能够用例如以下方法去转换: (UIColor *)RGBColorFromHexString:(NSStrin ...

  3. UI_UIImagePickerController(读取图片)

    创建图片 #pragma mark - 创建 photoImageView - (void)createphotoImageView { self.photoImageView = [[UIImage ...

  4. JDBC连接mysql时出现的ssl问题

    使用MySQL数据库时出现如下错误: WARN: Establishing SSL connection without server's identity verification is not r ...

  5. Redis常用命令速查 <第二篇>【转】

    一.Key Key命令速查: 命令 说明 DEL 删除给定的一个或多个 key,不存在的 key 会被忽略,返回值:被删除 key 的数量 DUMP 序列化给定 key,返回被序列化的值,使用 RES ...

  6. HD-ACM算法专攻系列(4)——A == B ?

    题目描述: 源码: /**/ #include"iostream" #include"string" using namespace std; string S ...

  7. JavaScript学习——JS事件总结

    回顾之前已经使用过的事件 (onsubmit.onclick.onload.onfocus.onblur.onmouseover.onmouseout) onfocus/onblur:聚焦离焦事件,用 ...

  8. 转:IE 无法使用 js trim() 的解决方法

    http://hi.baidu.com/yuiezt/item/756d0f4ec4d2640ec11613f9   var aa = $("#id").val().trim()  ...

  9. Python3基础笔记---模块

    参考博客:Py西游攻关之模块 模块的概念: 我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多编程语言都采用这种组织代码的方式.在Python中,一个.py文件就称之为 ...

  10. Linxu基本指令

    一.Linux权限的概念 Linux下有两种用户:普通用户和超级用户(). 普通用户:在linux下做有限的事情: 超级用户:可以在linux系统下做任何事情,不受限制. 普通用户的提示符是“$”,超 ...