Two

 

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] 表示以i结尾 j结尾的子序列的 答案数

  n^2的转移

   假设当前为 a[i] == b[j] , 那么它可以继承的 就是 所有 的 i,j组合 +1

   a[i] != b[j] 则 当前dp[i][j] = 0 咯

  第一中继承 只需要利用前缀 优化就行

  最后统计答案的话 就是sum[n][m]咯

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 1e3+, M = 2e2+, inf = 2e9, mod = 1e9+;
typedef long long ll; ll dp[N][N],sum[N][N],ans = ;
int a[N],b[N],n,m;
int main()
{
while (scanf("%d%d", &n, &m)!=EOF) {
for (int i=;i<=n;i++) scanf("%d", &a[i]);
for (int i=;i<=m;i++) scanf("%d", &b[i]);
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (a[i]==b[j]) { dp[i][j]=(sum[i-][j-]+)%mod; sum[i][j]=(-sum[i-][j-]+dp[i][j]%mod+sum[i-][j]%mod+sum[i][j-])%mod; } else sum[i][j]=(-sum[i-][j-]%mod+sum[i-][j]%mod+sum[i][j-])%mod;
ans=;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++) ans=(ans+dp[i][j])%mod;
printf("%I64d\n", (ans+mod)%mod);
}
return ;
}

  

HDU 5791 Two DP的更多相关文章

  1. hdu 5791 思维dp

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

  2. hdu 5791 (DP) Two

    hdu 5791 Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  4. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  5. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  6. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  9. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

随机推荐

  1. C++基础知识面试精选100题系列(21-30)[C++ basics]

    [本文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html [题 ...

  2. 【转】Git如何Check Out出指定文件或者文件夹

    [转]Git如何Check Out出指定文件或者文件夹http://www.handaoliang.com/a/20140506/195406.html 在进行项目开发的时候,有时候会有这样的需求那就 ...

  3. Apache2.4.6 添加虚拟主机

    apache2.4 与 apache2.2 的虚拟主机配置写法有所不同 apache2.2的写法: <VirtualHost *:80> ServerName domain.com Doc ...

  4. Maven 3.3.3 Win10环境下的使用实例(中)

    继上一篇文章介绍了Maven在Windows中的安装,本文将介绍 Maven 的核心概念. POM (Project Object Model) Maven 插件 Maven 生命周期 Maven 依 ...

  5. [转]Git远程操作详解

    原文:http://www.ruanyifeng.com/blog/2014/06/git_remote.html Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多 ...

  6. 使用CSS中margin和padding的基础和注意事项

    在CSS中,margin和padding是页面布局的主要属性,如何灵活有效使用对于基于DIV+CSS设计网页方法是非常重要的,笔者经常使用且经常误使用,所以根据经验和网上资料整理出切合自己的内容,以备 ...

  7. 两个oracle之间建立db link

    create database link tobsms connect to bjlt identified by bjlt using '(DESCRIPTION = (ADDRESS_LIST = ...

  8. [Android进阶]学习AccessibilityService实现微信抢红包插件

    在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...

  9. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  10. Java多线程题库

    一.    填空题 处于运行状态的线程在某些情况下,如执行了sleep(睡眠)方法,或等待I/O设备等资源,将让出CPU并暂时停止自己的运行,进入____阻塞_____状态. 处于新建状态的线程被启动 ...