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. json_encode和json_decode

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  2. Android 另类方法监听软键盘的弹出收起事件

    http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...

  3. mysql按照中文名称排序

    mysql按照中文名称排序   Sql代码  www.2cto.com   /*   Navicat MySQL Data Transfer      Source Server         : ...

  4. MVC中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别

    Html.RenderPartial与Html.RenderAction这两个方法都是用来在界面上嵌入用户控件的. 1. Html.RenderPartial是直接将用户控件嵌入到界面上: <% ...

  5. RelativeLayout_布局

    RelativeLayout布局 android:layout_marginTop="25dip" //顶部距离 android:gravity="left" ...

  6. java gui 下拉框中项删除按钮

    http://www.cnblogs.com/kangls/archive/2013/03/21/2972943.html http://m.blog.csdn.net/blog/ycb1689/74 ...

  7. UNIX网络编程-send、recv、sendto、recvfrom详解

    send.recv和sendto.recvfrom,一般情况下,send.recv在TCP协议下使用,sendto.recvfrom在UDP协议下使用,也可以在TCP协议下使用,不过用的很少. 1.s ...

  8. PNG图片压缩工具

    https://tinypng.com/ 效果非常不错. 340k的图能压缩到140k左右. 视觉效果差距不大

  9. 10. windows与linux文件共享

    1. 关闭防火墙 /etc/init.d/iptables stop 2. C:\Users\cfm>ping 192.168.232.131 正在 Ping 192.168.232.131 具 ...

  10. 执行powershell脚本

    打开powershell运行窗口: powershell.exe C:\Users\Administrator\Desktop\a.ps1 -a $a. -a $a :  vbs脚本路径(如 C:\h ...