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].(仔细琢磨琢磨,会发现很有道理)因为有减法的运算所以最后答案可能出现

负数,注意处理一下。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. const int mod = ;
  9. ll dp[][];
  10. int a[],b[];
  11.  
  12. int main()
  13. {
  14. int n,m;
  15. while (~scanf("%d%d",&n,&m)){
  16. for (int i= ; i<=n ; i++) scanf("%d",&a[i]);
  17. for (int i= ; i<=m ; i++) scanf("%d",&b[i]);
  18. memset(dp,,sizeof(dp));
  19. for (int i= ; i<=n ; i++){
  20. for (int j= ; j<=m ; j++){
  21. if (a[i]==b[j]) dp[i][j]=dp[i-][j]+dp[i][j-]+;
  22. else dp[i][j]=dp[i-][j]+dp[i][j-]-dp[i-][j-];
  23. dp[i][j]%=mod;
  24. }
  25. }
  26. printf("%I64d\n",(dp[n][m]+mod)%mod);
  27. }
  28. return ;
  29. }
 

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. 11 TCP/IP 基础与Linux的网络配置

    1. TCP/IP与OSI参考模型 TCP/IP是Unix/Linux世界的网络基础,在某种意义上Unix网络就是TCP/IP,而TCP/IP就是网络互联的标准.它不是一个独立的协议,而是一组协议.其 ...

  2. php 二维数组(没啥技术含量)

    <?php $cars = array( array('benchi',20,18), array('baoma',30,21), array('aodi',23,9) ); echo $car ...

  3. WS-Security

    ylbtech-Miscellaneos: WS-Security A,返回顶部 1, WS-Security (Web服务安全) 是一种提供在Web服务上应用安全的方法的网络传输协议. 2004年4 ...

  4. MySQL中四舍五入的实现

    MySQL四舍五入的实现   文章主要描述的是MySQL四舍五入的实际应用, 以及在其实际操作中的值得我们大家注意的事项与其实际应用代码的描述,以下就是文章的主要内容的详细描述,望大家在浏览之后会对其 ...

  5. 文件上传时jquery.form.js中提示form.submit SCRIPT5: 拒绝访问

    利用其它控件触发file的click事件来选择文件后,使用jquery.form.js中的submit方法提交时IE报错:form.submit SCRIPT5: 拒绝访问,其它浏览器正常, < ...

  6. sql查询当前月内的所有日期

    ),),)) as dt from master..spt_values where type='P' ),),),,)')

  7. DestroyWindow函数注意事项

    最近遇到这样一个问题:将一个窗口句柄以参数的形式传递给一个线程,在线程中使用完之后要将窗口销毁,调用DestroyWindow销毁窗口是返回false,GetLastError的结果为5:拒绝访问,而 ...

  8. MoleHill Getting Started AGAL(转)

    1.The OpCode This is what AGAL looks like: //vertex shader m44 op, va0, vc0 // pos to clipspace mov ...

  9. mac java目录

    /Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home mac java的安装目录为 /Library/Java/JavaVir ...

  10. APPLICATION ERROR #1502 .

    mantisbt出现1502问题解决:引起问题的原因:当提交的问题有分类,此时删除此分类,就会出现下面的情况.问题描述:APPLICATION ERROR #1502 没有找到类别.请使用浏览器的“返 ...