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. php接口和抽象类

    接口关键字:interface,不加class关键字接口里面有成员方法,但是没有函数体.实现接口使用的关键字:implements 不是extends子类必须实现接口的所有方法 使用接口,你可以指定某 ...

  2. 用JavaScript动态加载CSS和JS文件

    本文转载自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/11/14/2248451.html 今天项目中需要用到动态加载 CSS 文件 ...

  3. 关于jsp中response.sendRedirect显示错误

    今天在jsp中作判断时,当不同条件时利用response.sendRedirect(“url”)来转向不同的页面,首先是判断验证码,当错误时就转向错误页面:当正确时,才进行用户名和密码的判断,同样也r ...

  4. java io流 数据流传输

    java io流 数据流传输 把这段当公式用就可以了 //数据流传输 import java.io.*; public class Index{ public static void main(Str ...

  5. ASP.NET中进行消息处理(MSMQ) 三(转)

    在本文的前两篇文章里对MSMQ的相关知识点进行了介绍,很多阅读过这前两篇文章的朋友都曾问到过这样一些问题:  1.如何把MSMQ应用到实际的项目中去呢?  2.可不可以介绍一个实际的应用实例?  3. ...

  6. javascript的事件

    前戏 今天在博客中看到了javascript的事件机制,就自己试试写一个简单的冒泡捕获测试,但是测试结果出乎了我的意料,主要是自己原来对事件了解不是很清楚,现在写篇博客记录下. 基础 先来看一下我在A ...

  7. Js 扩展

    计算字符串的字节长度 String.prototype.len = function () { return this.replace(/[^\x00-\xff]/g, 'xx').length; } ...

  8. attr与prop

    Jquery获取checkbox属性checked为undefined (-- ::)转载▼ 标签: js jquery checkbox checked undefined 分类: JQuery 使 ...

  9. git学习系列--六分之一

    版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统,可以对任何类型的文件进行版本控制. 细说分布式版本控制系统 在这类系统中,像 Git,Mercurial,Bazaar 以 ...

  10. adb devices offline 问题大总结

    遇到doc对话框中adb devices ,一直显示设备处于offline状态,各种搜后安装所谓的: 1.开启usb调试模式2.关闭第三方手机助手软件3.重启adb服务    adb kill-ser ...