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. 锤子Smartisan T1手机官方4.4.2系统内核版本号信息

    从锤子smartisan T1手机官方系统EGL中获取内核版本号信息(由cofface提供): I/Adreno-EGL(  816): <qeglDrvAPI_eglInitialize:41 ...

  2. SolidEdge 工程图中如何显示彩色工程图

    点击这个按钮,然后更新视图   效果如下图所示,注意如果你的装配图(.dft文件)是单独拷贝出来的,装配图所引用的零件无法追溯到,则无法渲染这些零件,因此无法制作彩色工程图.

  3. Eclipse - 循环cin的输出怎样终止

    循环cin的输出怎样终止 本文地址: http://blog.csdn.net/caroline_wendy Eclipse中, 使用CDT编写C++代码时, 循环(while)cin输入程序, 须要 ...

  4. 【转载】读懂IL代码就这么简单(二)

    一 前言 IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...

  5. 数据结构与算法之贪心算法 C++实现

    1.基本思路:从问题的某一个初始解触发逐步逼近给定的目标,以尽可能快的求得更好的解. 当达到算法中某一步不能再继续前进时.就停止算法,给出近似值.也就是说贪心算法并不从总体最优考虑,它所作出的选择仅仅 ...

  6. MySQL搭建系列之多实例

    所谓多实例.就是在一台server上搭建.执行多个MySQL实例,每一个实例使用不同的服务port.通过不同的socket监听:物理上,每一个实例拥有独立的參数配置文件及数据库. 通常情况下.一台se ...

  7. Oracle RAC cache fusion原理測试

    Oracle RAC cache fusion是RAC最核心的工作机制.他把全部实例的SGA虚拟成一个大的SGA区,每当不同的实例请求同样的数据块,这个数据块就须要在实例间进行传递. 那究竟什么时候传 ...

  8. UBUNTU安装PHP,即所谓得LAMP

    Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组经常使用来搭建动态站点或者server的开源软件,本身都是各自独立的程序,可是由于常被放在一起使用.拥有了越来越 ...

  9. Creating a .bash_profile on your mac

    A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...

  10. iOS 各种编译错误汇总

    1.error: macro names must be identifiers YourProject_prefix.pch 原因: 因为你弄脏了预处理器宏,在它处于<Multiple Val ...