Two
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1533 Accepted Submission(s): 676Problem DescriptionAlice 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.InputThe 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.
OutputFor each test case, output the answer mod 1000000007.Sample Input3 2
1 2 3
2 1
3 2
1 2 3
1 2Sample Output2
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 ;
}
随机推荐
- oracle存储过程中使用字符串拼接
1.使用拼接符号“||” v_sql := 'SELECT * FROM UserInfo WHERE ISDELETED = 0 AND ACCOUNT =''' || vAccount || '' ...
- python type
基于2.7 版本 type 是内置函数,有两种用法 class type(object) With one argument, return the type of an object. The re ...
- Java 利用DFA算法 屏蔽敏感词
原文:http://www.open-open.com/code/view/1435214601278 import java.io.BufferedReader; import java.io.Fi ...
- sqlalchemy的merge使用
1.先看下文档 merge(instance, load=True) Copy the state of a given instance into a corresponding instance ...
- 【Todo】Java的JIT机制
先是参考了这篇说的不怎么详细的文章<Java的JIT机制>(Link) JIT是just in time,即时编译技术.使用该技术,能够加速java程序的执行速度. 通常javac将程序源 ...
- nginx配置初步
nginx配置初步 1,切换至nginx目录,找到配置文件目录 cd /etc/nginx/conf.d 2,拷贝一份conf文件 sudo cp default.conf head.conf 3,进 ...
- Handlebars的基本用法 Handlebars.js使用介绍 http://handlebarsjs.com/ Handlebars.js 模板引擎 javascript/jquery模板引擎——Handlebars初体验 handlebars.js 入门(1) 作为一名前端的你,必须掌握的模板引擎:Handlebars 前端数据模板handlebars与jquery整
Handlebars的基本用法 使用Handlebars,你可以轻松创建语义化模板,Mustache模板和Handlebars是兼容的,所以你可以将Mustache导入Handlebars以使用 Ha ...
- TagCanvas - HTML5 Canvas Tag Cloud
http://www.goat1000.com/tagcanvas.php watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmlja3JvcHJhaw==/ ...
- Hibernate 配置C3P0 连接池
第一步在hibernate.cfg.xml配置 <!-- 连接池 --> <property name="hibernate.connection.provider_cla ...
- Linux 用户和文件权限管理
Linux —— 用户权限管理 权限: 为什么需要权限管理? 1.计算机资源有限,我们需要合理的分配计算机资源. 2.Linux是一个多用户系统,对于每一个用户来说,个人隐私的保护是十分重 ...