题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282

Senior's String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 142    Accepted Submission(s): 40

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。

她很开心,但在开心之余她还想考考学弟。

她定义L为X与Y的最长公共子序列的长度(子序列在字符串内不一定连续。一个长度为L的字符串有2L个子序列,包含空子序列)。

如今学姐姐取出了X的全部长度为L的子序列。并要求学弟回答在这些子序列中,有多少个是Y的子序列。

由于答案可能很大,所以学弟仅仅须要回答终于答案模109+7。

官方题解:

wei里存的是Y串的前j个字母中,  26个字母最后出如今第几个(字符串下标+1)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX_N 1100//再大dp数组会爆的 O(n*m)
char a[MAX_N],b[MAX_N];
int dp[MAX_N+1][MAX_N+1];
int dd[MAX_N+1][MAX_N+1];//b前j个字母中 最后一个 与x[i]
int wei[MAX_N+1][26];//Y前i个字母中 最后一个j字母在的位置
__int64 f[MAX_N+1][MAX_N+1]; int mod=1e9+7;
int main()
{
int n,m,tem,t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",a,b);
memset(dp,0,sizeof(dp));
n=strlen(a);
m=strlen(b);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i]==b[j])
dp[i+1][j+1]=max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1]));
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][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]%mod);
}
return 0;
}

hdu 5282 Senior&#39;s String 两次dp的更多相关文章

  1. HDU 5280 Senior&#39;s Array 最大区间和

    题意:给定n个数.要求必须将当中某个数改为P,求修改后最大的区间和能够为多少. 水题.枚举每一个区间.假设该区间不改动(即改动该区间以外的数),则就为该区间和,若该区间要改动,由于必须改动,所以肯定是 ...

  2. HDU 5281 Senior&#39;s Gun 杀怪

    题意:给出n把枪和m个怪.每把枪有一个攻击力,每一个怪有一个防御力.假设某把枪的攻击力不小于某个怪的防御力则能将怪秒杀,否则无法杀死.一把枪最多仅仅能杀一个怪,不能用多把枪杀同一个怪.每杀一次怪能够得 ...

  3. HDU 5281 Senior&#39;s Gun

    Senior's Gun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)

    Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. HDU 5280 Senior&#39;s Array

    Senior's Array Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  6. hdu 4865 Peter&#39;s Hobby(概率dp)

    http://acm.hdu.edu.cn/showproblem.php? pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,各自是每种天气下叶子呈现状态的概率和今天天气对明天天气的 ...

  7. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...

  8. hdu 2476"String painter"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给定字符串A,B,每次操作可以将字符串A中区间[ i , j ]的字符变为ch, ...

  9. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

随机推荐

  1. 基于visual Studio2013解决面试题之0807strstr函数

     题目

  2. mssql 返回表的创建语句

    if OBJECT_ID('sp_create_table_sql','P') is not null drop proc sp_create_table_sql go create proc sp_ ...

  3. 解决 Android SDK Manager不能下载旧版本的sdk的问题

    解决无法使用Android SDK  Manager下载SDK开发包的解决办法. 当我们在官网下载google的集成ADT,也就是adt-bundle-linux-x86.zip开发包,进行解压, 打 ...

  4. android 5.0新特性

    Android Lollipop 面向开发人员的主要功能 Material Design 设计 注重性能 通知 以大屏幕呈现 以文档为中心 连接性能再上一级 高性能图形 音频处理功能更强 摄像头和视频 ...

  5. Linux红黑树(二)——访问节点

    核心对红黑树使用两点说明 1.头文件 <Documentation/rbtree.txt> Linux's rbtree implementation lives in the file ...

  6. VC 绘图技巧--自定义形状图形

    自定义形状图形,定义几个点围城的图形,然后进行描边和填充: if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1 ...

  7. MPICH3环境配置

    最新版的mpich简化了运行方式,不再提供mpd开头的命令,只需要一个mpiexec即可启动mpi运行环境,方便了mpi编程.源代码下载地址:http://www.mpich.org/download ...

  8. JPA的Embeddable注解

    来源于http://zjsword2000.blog.163.com/blog/static/4583983320083184844734/ 在hibernate中实现自定义类型,只要实现UserTy ...

  9. 马航MH17事件将把普京逼入绝境?

    据7月22日报道,马克兰东部民间武装22日凌晨将失事客机的"黑匣子"交给马来西亚方面.乌政府与民间武装允许在坠机地点附的小范围停火. 与此同一时候,联合国安理会21日通过决议,敦促 ...

  10. 当try和finally里都有return时,会忽略try的return,而使用finally的return

    今天去逛论坛 时发现了一个很有趣的问题: 谁能给我我解释一下这段程序的结果为什么是:2.而不是:3 代码如下: class Test { public int aaa() { int x = 1; t ...