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. 一个很奇怪的重复链接lib的问题

    早上在调一个程序的时候感觉非常奇怪,就是数据在初始化的时候会失败,后来发现是获取一个数据的时候出错了 假设我们又一个config.lib,sql.dll和main.exe 因为数据库在打开数据库的时候 ...

  2. case when完成不同条件的显示

    好长时间不写sql了,今天群里问了一个问题: 我想得到的就是  2,3,4,就是低,5,6,7,8,9就是中,10,11....就是高,现在只知道zpcs,怎么得到高中低? case 写的,decod ...

  3. Hibernate5.2之原生SQL查询

    Hibernate5.2之原生SQL查询 一. 介绍  在上一篇博客中笔者通过代码的形式给各位读者介绍了Hibernate中最重要的检索方式--HQL查询.在本博文中笔者将向各位读者介绍Hiberna ...

  4. iOS中定时器的使用

    1. NSTimer 不是很精确 2.CADisplayLink 屏幕 3.通过GCD来实现定时间器 //定时循环执行事件 //dispatch_source_set_timer 方法值得一提的是最后 ...

  5. 深入理解 Laravel Eloquent(三)——模型间关系(关联)

    Eloquent是什么 Eloquent 是一个 ORM,全称为 Object Relational Mapping,翻译为 "对象关系映射"(如果只把它当成 Database A ...

  6. c# 高效率导出多维表头excel

    [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThread ...

  7. java和Javascript的区别

    1 首先,这两个家伙没有任何的血缘关系,java是是由Sun 公司于1995年5月推出的,而javascript是于1995年由Netscape公司设计实现而成的,由于Netscape公司与Sun公司 ...

  8. C++ 如何有效地使用对话框

    Q:如何在对话框中加入工具条 在 OnInitDialog 中加入下面代码: BOOL CYourDlg::OnInitDialog() { CDialog::OnInitDialog(); // C ...

  9. 编程工具系列之二------使用GDB的源代码查看功能

    在调试程序的过程中,可以自由地查看相关的源代码(如果有源代码的话)是一项最基本的特性.      一些IDE在这方面做得相当好,GDB当然也提供了这项特性,虽然不如IDE直观,但在一定程度上要比IDE ...

  10. PL/SQL 查找1-100之间的素数

    --实现查找1-100之间的素数 declare v_no ; --标记是第几个素数 v_number number; --表示1-100之间的数 v_num number; begin .. loo ...