题链: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. [置顶] gis海量资源网盘提供VIP账号无广告高速下载 (更新更多资源)

    资源网盘下载地址:http://laoheitan.bego.cc/ 城通网盘 vip帐号共享 省去 烦人的 广告  多任务同时下载 独乐乐 不如众乐乐 好人 勿改密码. 获取到 vip下载连接后 请 ...

  2. LeetCode: LRU Cache [146]

    [题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...

  3. HOOK自绘原理 good

    做“HOOK文件打开/保存对话框”的过程中,我首先研究了界面库的相关知识.界面库一般都是由C/C++这种中低级语言编码,这是因为在Windows下的界面库实现技术大都以直接操作控制Windows的消息 ...

  4. boost.asio系列——buffer

    创建buffer 在io操作中,对数据的读写大都是在一个缓冲区上进行的,在asio框架中,可以通过asio::buffer函数创建一个缓冲区来提供数据的读写.buffer函数本身并不申请内存,只是提供 ...

  5. Linux下经常使用的shell命令记录

    硬件篇 CPU相关 lscpu #查看的是cpu的统计信息. cat /proc/cpuinfo #查看CPU信息具体信息,如每一个CPU的型号,主频等 内存相关 free -m #概要查看内存情况 ...

  6. c++ inheritance -- 继承

    c++ inheritance -- 继承 终于要决心弄明白继承了,以前仅限于大学时学习,以后工作也没有用,现在就依照(百度百科)文章写些测试的代码. 文章说 ==================== ...

  7. hdu 4715 Difference Between Primes (打表 枚举)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. 怎样将baidu地图中的baidu logo 去掉

    今天我的老大问我是不是能够将baidumap 的js版中baidu logo 去掉. 我上网查询一下,有各种方法.比方将相应的logo div remove hide 等等,这些都是须要JS 函数触发 ...

  9. ios block循环引用问题

    ios开发中,开了ARC模式,系统自动管理内存,如果程序中用到了block就要注意循环引用带来的内存泄露问题了 这几天遇到一个问题,正常页面dismiss的时候是要调用dealloc方法的,但是我的程 ...

  10. thinkPHP框架介绍(一)

    原文:thinkPHP框架介绍(一) 一.ThinkPHP的介绍 期间有对ThinkPHP框架在学习上的问题欢迎大家交流:QQ:812231134 MVC M - Model 模型           ...