hdu 5791

Two

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1421    Accepted Submission(s): 630

Problem Description
Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A' is a subsequence of A. B' is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.
 
Input
The input contains multiple test cases.

For each test case, the first line cantains two integers N,M(1≤N,M≤1000). The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.

 
Output
For each test case, output the answer mod 1000000007.
 
Sample Input
3 2
1 2 3
2 1
3 2
1 2 3
1 2
 
Sample Output
2
3
 

求两个序列的相同的公共子序列的个数,取模。

DP[i][j]表示到a数组的第 i 个和b数组的第 j 个之间的个数,可以归纳出,当a[i]==b[j]时,dp[i][j]=dp[i-1][j]+dp[i][j-1]+1;

当不等的时候,dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1].(仔细琢磨琢磨,会发现很有道理)因为有减法的运算所以最后答案可能出现

负数,注意处理一下。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
const int mod = ;
ll dp[][];
int a[],b[]; int main()
{
int n,m;
while (~scanf("%d%d",&n,&m)){
for (int i= ; i<=n ; i++) scanf("%d",&a[i]);
for (int i= ; i<=m ; i++) scanf("%d",&b[i]);
memset(dp,,sizeof(dp));
for (int i= ; i<=n ; i++){
for (int j= ; j<=m ; j++){
if (a[i]==b[j]) dp[i][j]=dp[i-][j]+dp[i][j-]+;
else dp[i][j]=dp[i-][j]+dp[i][j-]-dp[i-][j-];
dp[i][j]%=mod;
}
}
printf("%I64d\n",(dp[n][m]+mod)%mod);
}
return ;
}
 

hdu 5791 (DP) Two的更多相关文章

  1. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  3. HDU 5791 Two (DP)

    Two 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 Description Alice gets two sequences A and ...

  4. HDU 5791:Two(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description   Alice gets two sequences A ...

  5. HDU 5791 Two DP

    Two   Problem Description   Alice gets two sequences A and B. A easy problem comes. How many pair of ...

  6. hdu 5791 Two 二维dp

    Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  7. hdu 5791 思维dp

    题目描述: 求序列A,B的公共子序列个数: 基本思路: 想到了dp,选的状态也对,但是就是就是写不出状态转移方程,然后他们都出了,到最后我还是没出,很难受,然后主要是没有仔细考虑dp[i][j],dp ...

  8. HDU 5791 Two(LCS求公共子序列个数)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5791 题意: 给出两个序列,求这两个序列的公共子序列的总个数. 思路: 和LCS差不多,dp[i][ ...

  9. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

随机推荐

  1. 【MySQL】探究之常用SQL

    一些SQL命令(不断更新,我总记不住,哭) List 建库建表 表的重命名(不区分大小写) 列的重命名 编码 修改结构 添加删除索引 大批量删除 binlog相关 select相关 数据库备份和恢复 ...

  2. 建工财务搬家NC变更|rman各种测试|

    1,使用全备份之后的还原不需要建立表空间. 2,归档日志备份之后,使用delete all input,在backup database plus achivelog之后,会在完成备份之后自动删除归档 ...

  3. RMAN_学习笔记3_RMAN Catalog恢复目录

    2014-12-23 Created By BaoXinjian

  4. linux DMZ host 允许虚拟机以Host-only的方式上网

    linux DMZ host 允许虚拟机以Host-only的方式上网. host ip 192.168.0.17 vboxnet0 ip 192.168.56.1 1.首先打开linux的转发功能: ...

  5. [poj1860] Currency Exchange (bellman-ford算法)

    题目链接:http://poj.org/problem?id=1860 题目大意:给你一些兑换方式,问你能否通过换钱来赚钱? 使用ford算法,当出现赚钱的时候就返回YES,如果不能赚钱,则返回NO ...

  6. LR11-更改licence

    准备工作: 超级licence 6.5w:AEACFSJI-YJKJKJJKEJIJD-BCLBR AEACFSJI-YJKJKJJKEJIJD-BCLBR 操作步骤: 1.网上下载lr删除注册表工具 ...

  7. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

    今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...

  8. Android Service 文档

    应用场景: 1  用于将后台逻辑(Service中)和UI逻辑(Activity中)进行解耦,实现Service功能的复用,为其他程序提供功能. 2  后台功能,由于Activity在进入后台时(On ...

  9. 在python中处理XML

    XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下: <data> <country name="Liechtenstein"> < ...

  10. 1.No MBR错误

    如果提示如下错误: Error: No MBR is found at SD/MMC.                                              Hint: use f ...