Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1533    Accepted Submission(s): 676

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位的答案是多少。

那么dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1] ,需要特别判断a[i]=b[j]时,dp[i][j]+=dp[i-1][j-1]+1。

附AC代码:

 #include<bits/stdc++.h>
using namespace std; const int pr=; int dp[][];
int a[],b[]; int main(){
int n,m;
while(cin>>n>>m){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
dp[i][j]=;
}
}
for(int i=;i<=n;i++){
cin>>a[i];
}
for(int i=;i<=m;i++){
cin>>b[i];
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
dp[i][j]=dp[i][j-]+dp[i-][j]-dp[i-][j-];
if(a[i]==b[j])
dp[i][j]+=dp[i-][j-]+;
if(dp[i][j]<)
dp[i][j]+=pr;
if(dp[i][j]>=pr)
dp[i][j]%=pr;
}
}
cout<<dp[n][m]<<endl;
}
return ;
}

随机推荐

  1. Obj-C, library with ARC code and warning - Method possibly missing a [super dealloc] call?

    1 down vote favorite I'm adding the MKStoreKit to my app and I'm getting a warning, Method possibly ...

  2. cms完整视频教程+源码 孔浩老师 全131讲

    话不多说,请看如下链接,  项目在此文件夹目录下:  JAVA专区>3.深入Java Web>3.1.cms项目 很多反馈说无效 本次 2016.09.12 发布最新链接 链接:https ...

  3. BUPT复试专题—Special 数(2017)

    题目描述 设一个正整数既是平方数乂是立方数时,称为Special数. 输入 输入包含多组测试数据,笫1行输入测试数据的组数,接下来在后续每行输入n(n<=1000000000) 输出 输出1到n ...

  4. hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

  5. IntelliTrace窗口无法弹出的解决办法

    最近在使用EF框架,所以需要IntelliTrace窗口进行对ADO的SQL生成监控.可找了半天都无法Call出该窗口. 在Debug模式下,选择调试->窗口 里面根本没有IntelliTrac ...

  6. iOS开发核心语言Objective C —— 面向对象思维、setter和getter方法及点语法

    本分享是面向有意向从事iOS开发的伙伴们.或者已经从事了iOS的开发人员.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基础,建议您先翻阅我之前分享的iOS ...

  7. HDU 1398 Square Coins(母函数或dp)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. HDU 1085 Holding Bin-Laden Captive!(母函数,或者找规律)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. FIR300M刷openwrt

    淘宝看到一款FIR300M路由器,当时只要19.9元.图便宜就买了. Hardware Architecture: MIPS Vendor: MediaTek (Ralink) Bootloader: ...

  10. 编程算法 - 数组中出现次数超过一半的数字 代码(C)

    数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. ...