本文转自:http://blog.csdn.net/queuelovestack/article/details/52096337

题意:

给你两个序列A和B

问两个序列有多少个子序列一样

例如{1,2}与{1,2}一样,{1,2,4}与{1,4,2}不一样

题解:

很显然的一道DP题

求的是公共子序列对数

令dp[i][j]表示A序列前i个数和B序列前j个数的相同子序列对有多少个

状态转移方程为dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+(a[i]==b[j]?dp[i-1][j-1]+1:0)

怎么理解呢?对于序列A,当加入第i个数时,它增加了长度为j的序列B中与该数相同的数,序列B同理

还有增加的取决于a[i]是否等于b[j],若相等,则增加了dp[i-1][j-1]+1对,这个1就是(a[i],b[j])这对,dp[i-1][j-1]则是有共同前缀的对

dp还是要好好理解一下,毕竟还是比较常见,不会很吃亏,本人就是一个很好的例子,总是在dp上吃亏

【时间复杂度&&优化】

O(n^2)

大牛就是大牛,显然就dp了,当我看的时候,看了那么长时间都不知道是dp,我好渣啊 >_< 注意:子序列,模1e9 的一些问题有可能就是dp!

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
#define PI(A) cout<<A<<endl
#define SI(N) cin>>N
#define SII(N,M) cin>>N>>M
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
#define dbg(x) cout <<#x<<" = "<<x<<endl
#define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
#define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
const double EPS= 1e-9 ; /* ///////////////////////// C o d i n g S p a c e ///////////////////////// */ const int MAXN= 1000 + 9 ; int A[MAXN],B[MAXN];
ll dp[MAXN][MAXN];
int n,m;
int MOD= 1000000007; int main()
{
while(SII(n,m))
{
//写DP数组尽量从1开始,因为dp的话一般会用到上个状态,为了不让数组越界,所以从1开始
Rep(i,1,n) SI(A[i]);
Rep(i,1,m) SI(B[i]);
Rep(i,1,n)
Rep(j,1,m)
{
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+(A[i]==B[j]?dp[i-1][j-1]+1:0);
dp[i][j]%=MOD;
}
//一定要注意 有减法的取模时一定要判断答案的正负,如果是负的就+MOD
PI((dp[n][m]>0?dp[n][m]:dp[n][m]+MOD));
}
return 0;
}

2016 Multi-University Training Contest 5 Two的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  5. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  6. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  7. 2016 Al-Baath University Training Camp Contest-1 G

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

  8. 2016 Al-Baath University Training Camp Contest-1 F

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

  9. 2016 Al-Baath University Training Camp Contest-1 D

    Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...

  10. 2016 Al-Baath University Training Camp Contest-1 C

    Description Rami went back from school and he had an easy homework about bitwise operations (and,or, ...

随机推荐

  1. scala言语基础学习三(面向对象编程)

    定义一个简单的类 //定义类,包含field以及方法 自定义的getter 和setter 仅仅暴露field的getter和setter方法 private[this]的使用 (只能在当前实例中使用 ...

  2. Android——AutoCompleteTextView、Spinner和消息提示

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  3. POJ-2886 Who Gets the Most Candies?(线段树+模拟)

    题目大意:n个小孩按顺时针站成一圈,每次会有一个小孩出队(第一个出队的小孩已知),在他出队时会指定下一个出队的小孩,直到所有的小孩全部出队游戏结束.第p个出队的小孩会得到f(p)个糖果,f(p)为p的 ...

  4. HDU-4276 The Ghost Blows Light (树形DP+背包)

    题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...

  5. poj1094 拓扑序

    题意:现在有多个大写字母(不一定连续),给出字母之间的大小关系,问到第几个关系时就能判断有唯一大小排序或出现矛盾,或是有多个合理排序,若有唯一排序,则输出它. 拓扑序,只不过坑爹的是如果关系处理到一半 ...

  6. Linux磁盘空间被未知资源耗尽【转】

    Linux磁盘空间被未知资源耗尽 在linux中,当我们使用rm在linux上删除了大文件,但是如果有进程打开了这个大文件,却没有关闭这个文件的句柄,那么linux内核还是不会释放这个文件的磁盘空间, ...

  7. sql server 常用的系统存储过程

      系统存储过程 说明 sp_databases 列出服务上的所有数据库 sp_helpdb 报告有关指定数据库或所有数据库的信息 sp_renamedb 更改数据库的名称 sp_tables 返回当 ...

  8. 【转】关于B/S架构应用程序的权限设置分析和总结

    来自:http://www.cnblogs.com/zhouxunyu/p/3790122.html 分析:不同的用户登录到系统后赋予不同的操作权限,而用户存在于数据库中,标识用户权限的字段也保存在数 ...

  9. wikioi 1075 明明的随机数

    /*============================================================ 1075 明明的随机数 题目描述 Description 明明想在学校中请 ...

  10. 重新加载maven项目的依赖项

    最近在调试reportNG,测试允许完以后,报告总是使用的testNG的格式,并且只有index和overview两个文件. 找了好多帖子,大家都是那么设置的都没有问题,难道是哥人品不好?错! 大家基 ...