hdu5282 最长公共子序列的变形
pid=5282">http://acm.hdu.edu.cn/showproblem.php?pid=5282
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.
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.
also is the subsequence of string Y modulo 109+7.
2
a
b
aa
ab
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 最长公共子序列的变形的更多相关文章
- DP专辑之最长公共子序列及其变形
vijos1111(裸的最长公共子序列) 链接:www.vijos.org/p/1111 题解:好久没有写最长公共子序列了,这题就当是复习了.求出最长公共子序列,然后用两个单词的总长度减去最长公共子序 ...
- HDU 1080 Human Gene Functions - 最长公共子序列(变形)
传送门 题目大意: 将两个字符串对齐(只包含ACGT,可以用'-'占位),按照对齐分数表(参见题目)来计算最后的分数之和,输出最大的和. 例如:AGTGATG 和 GTTAG ,对齐后就是(为了表达对 ...
- hdu1503 最长公共子序列变形
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...
- hdu 1080 dp(最长公共子序列变形)
题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G - G ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53414 Accepted: 18449 Desc ...
- poj 1080 Human Gene Functions (最长公共子序列变形)
题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...
随机推荐
- 获取ip,判断用户所在城市
PHP获取IP地址 这个比较简单了,利用PHP自带函数就可以了,PHP中文手册看一下,都有现成的例子,就不过多说明了,直接上代码,A段: <? //PHP获取当前用户IP地址方法 $xp_Use ...
- 51 nod 1693 水群
1693 水群 基准时间限制:0.4 秒 空间限制:524288 KB 分值: 160 难度:6级算法题 收藏 关注 总所周知,水群是一件很浪费时间的事,但是其实在水群这件事中,也可以找到一些有 ...
- HDU 4320 Contest 3
只需A的全部质因数包含在B中即可. #include <iostream> #include <cstdio> #define LL __int64 #include < ...
- struts.xml中出现extends undefined package struts-default解决的方法
在struts.xml中出现extends undefined package struts-default,经过查阅资料原来是由于没有联网的缘故. 这样解决:在myeclipse中关联本地的dtd文 ...
- @dynamic与@synthesize的差别
如今非常多时候我们都已经不再使用@synthesizekeyword了,可是须要了解当中的原理: 一.@dynamic与@synthesize的差别 @property有两个相应的词.一个是@synt ...
- zzulioj--1799--wrz的压岁钱(贪心)
1799: wrz的压岁钱 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 407 Solved: 71 SubmitStatusWeb Boa ...
- caffe中lenet_train_test.prototxt配置文件注解
caffe框架下的lenet.prototxt定义了一个广义上的LeNet模型,对MNIST数据库进行训练实际使用的是lenet_train_test.prototxt模型. lenet_train_ ...
- [JZOJ 5912] [NOIP2018模拟10.18] VanUSee 解题报告 (KMP+博弈)
题目链接: https://jzoj.net/senior/#contest/show/2530/2 题目: 众所周知,cqf童鞋对哲学有着深入的理解和认识,并常常将哲学思想应用在实际生活中,例如锻炼 ...
- 计算sigma
1.计算平均值Avg Avg = (a0 + a1 + ......+ an-1) / n 2.计算sigma sigma = sqrt( ( (a0-avg) ^2 + (a1-avg) ^2 ...
- QT笔记 -- (3) 为QLabel添加鼠标响应方法1
参考 http://qt-project.org/wiki/Make-a-QLabel-Clickable 1.首先重载QLabel的mousePressEvent,这样点击QLabel时就能发出cl ...