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. Portal

    https://chenliang0571.wordpress.com/2013/12/08/openwrt-wifidog-wifi-hotspots/http://www.h3c.com.cn/M ...

  2. selenium web driver 配合使用testng

    首先为eclipse添加testng插件 步骤如下:help->Install New SoftWare... 2. 添加testng链接,该链接可以在这里找到 For the Eclipse ...

  3. MySQL 5.6 my.cnf 模版

    [client] port = socket = /var/run/mysqld/mysqld.sock [mysqld_safe] thp-setting=never socket = /var/r ...

  4. We7 CMS研究

    我下载的we7 3.0是基于vs 2010的,官方网站也建议使用vs2010,但是我有追新的习惯,并相信vs 2013一定能够兼容vs2010的项目,于是在vs2013下打开解决方案并且全部升级,把目 ...

  5. java 基础第一季

    1. i安装jdk ii 配置环境变量:JAVA_HOME 配置jdk的安装路径 path            配置命令文件的位置 bin目录的安装路径 PATH_HOME 配置库文件的位置   l ...

  6. ajax加载表格数据

    一.html代码 <style type="text/css"> .table-taskinfo table tr { border-top: 2px solid #9 ...

  7. 【python】入门学习(七)

    设置字符串格式: format % values >>> x =/ >>> print(x) 0.012345679012345678 >>> p ...

  8. 手动关闭searchDisplayControlelr

    两行代码搞定 [searchBar endEditing:YES]; [searchDisplayControllersetActive:NO];

  9. 编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值

    编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值 1 #import <Foundation/Foundation.h>  2   3 int main( ...

  10. 与你相遇好幸运,Sails.js自定义responses

    在 /api/responses/ 新建文件 >serviceDBError.js 自定义的数据库错误 >serviceError.js  自定义的数据错误 >serviceSucc ...