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,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...
随机推荐
- Having用法
HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 和 SELECT 的交互方式类似.WHERE 搜索条件在进行分组操作之前应用:而 HAVING 搜索条件在进行分组操作之后应用 ...
- 【codeforces 505D】Mr. Kitayuta's Technology
[题目链接]:http://codeforces.com/problemset/problem/505/D [题意] 让你构造一张有向图; n个点; 以及所要求的m对联通关系(xi,yi) 即要求这张 ...
- Informatica PowerCenter使用介绍-转载
转载自:https://blog.csdn.net/wen_demon/article/details/44155639 1. INFORMATICA CLIENT的使用1.1 Repository ...
- 51 nod 1431 快乐排队
1431 快乐排队 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 有一群人在排队,如果某个人想排到前面去,可以花 ...
- Android在程序中浏览网页
本文是自己学习所做笔记,欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 有时须要在程序中浏览一些网页.当然了能够通过调用系统的浏览器来打开浏览.可是大多 ...
- PyQt: LineEdit的智能输入提示
使用的的类是QtGui.QCompleter from PyQt4 import QtGui,QtCore str = QtCore.QStringList(['a','air','airbus']) ...
- Java 实现适配器(Adapter)模式
平时我们会常常碰到这种情况,有了两个现成的类,它们之间没有什么联系.可是我们如今既想用当中一个类的方法.同一时候也想用另外一个类的方法.有一个解决方法是.改动它们各自的接口.可是这是我们最不愿意看到的 ...
- Codeforces 10A-Power Consumption Calculation(模拟)
A. Power Consumption Calculation time limit per test 1 second memory limit per test 256 megabytes in ...
- poj_1952最大下降子序列,统计个数
其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...
- Django(part4)
一个简单的form表单: #polls/templates/polls/detail.html<h1>{{ question.question_text }}</h1> {% ...