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 ;
}
随机推荐
- IE浏览器打不开解决的方法
windows 7和windows 8上的IE浏览器打不开.非常可能是权限问题,解决的方法: 点击"開始"-"执行",输入"regedit" ...
- 经典游戏“大富翁4”存档文件修改器Rich4Editor下载
下载地址: http://files.cnblogs.com/files/xiandedanteng/Rich4Editor20170614.zip http://files.cnblogs.com/ ...
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53414 Accepted: 18449 Desc ...
- Android-studio 连接真机 调试weex项目
1.选择项目 platforms / android 2.创建虚拟机(AVD) (1)点击 AVD Manager (2) 点击 Create Virtual Device 最后发现 CPU 不 ...
- SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)
--====================================================== --SQL基础-->层次化查询(START BY ... CONNECT BY ...
- C#不用union,而是有更好的方式实现 .net自定义错误页面实现 .net自定义错误页面实现升级篇 .net捕捉全局未处理异常的3种方式 一款很不错的FLASH时种插件 关于c#中委托使用小结 WEB网站常见受攻击方式及解决办法 判断URL是否存在 提升高并发量服务器性能解决思路
C#不用union,而是有更好的方式实现 用过C/C++的人都知道有个union,特别好用,似乎char数组到short,int,float等的转换无所不能,也确实是能,并且用起来十分方便.那C# ...
- 杭电1596find the safest road(spfa)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 新手必备的SEO优化工具
- 手游服务器php架构比较
从swoole项目开始到现在,一直有人在问这个问题.今天来抽空讲一下它.为什么swoole非要使用纯C来写而不是PHP代码来实现,核心的原因有2点: 1. PHP无法直接调用操作系统API 如send ...
- 深入解析Hibernate核心接口
Hibernate有很多值得学习的地方,这里我们主要介绍Hibernate核心接口,包括介绍SessionFactory接口.Query和Criteria接口等方面. Session 接口对于Hibe ...