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. DataTable得到某行某列的值

    DataTable dt=this.GetRepeatTableData("repeating1"); int count=dt.Rows.Count;for(int x=0;x& ...

  2. servlet 和filter 的生命周期说明

    servlet : 当客户端第一次访问servlet的时候,服务器就会创建servlet实例,servlet 就会执行init方法,每次请求,服务器会开一个新的线程访问servlet中得service ...

  3. markdown 基本语法

    代码块: ```console.log(1);```--- 标题: # h1## h2### h3 --- 粗斜体: *斜体***粗体*****粗斜体*** --- 强调:`强调` --- 链接:[百 ...

  4. sql server 行转列(转载)

    SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...

  5. 【轮子】发现一个效果丰富酷炫的Android动画库

    没有什么比发现一个好轮子更让人开心的了. 这个库分分钟提高交互体验 :AndroidViewAnimations 一张图说明一切 配置和使用也相当简单 GitHub地址

  6. 【python】入门学习(四)

    函数: 定义函数 #area.py from math import pi def area(radius): """Return the area of a circl ...

  7. Linux面试题汇总答案

    转自:小女生的Linux技术~~~Linux面试题汇总答案~~ 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的 ...

  8. 20145213《Java程序设计》第八周学习笔记

    20145213<Java程序设计>第八周学习笔记 教材学习内容总结 "桃花春欲尽,谷雨夜来收"谷雨节气的到来意味着寒潮天气的基本结束,气温回升加快.刚出冬的我对于这种 ...

  9. GCD的使用

    什么是 GCD Grand Central Dispatch (GCD) 是 Apple 开发的一个多核编程的解决方法.该方法在 Mac OS X 10.6 雪豹中首次推出,并随后被引入到了 iOS4 ...

  10. Android笔记:去除标题栏

    1: 在oncreate方法中添加requestWindowFeature(Window.FEATURE_NO_TITLE); 必须在setContentView()之前执行. 2: 在Android ...