本文转自: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. DELPHI相应鼠标滚轮

    在鼠标的MouseWheel事件里写入以下内容 if WheelDelta < 0 then    SendMessage(scrollBox1.Handle, WM_VSCROLL, SB_L ...

  2. JAVA常用关键字

    Java 中常用关键字: 一一解释(先以印象注明含义,若有错误或未填写的待用到后补充.更新):(蓝色为不确定部分) abstract : 虚类 boolean : 类型定义——布尔型 break : ...

  3. 关于for,while与do while

    Q:输入一个整数i,输出i+(i+1)+...+19+20的结果 S:法1:for #include<stdio.h> #include<math.h> #include< ...

  4. CSS中控制换行的四种属性

    一.white-space 可以实现HTML中PRE标签的效果,以及单元格的noWrap效果,点此查看示例. Example Source Code 语法: white-space : normal  ...

  5. 10046事件跟踪会话sql

    背景知识: 10046 事件按照收集信息内容,可以分成4个级别: Level 1: 等同于SQL_TRACE 的功能 Level 4: 在Level 1的基础上增加收集绑定变量的信息 Level 8: ...

  6. 开源app之MyHearts

    前言 这个月,说实话,有忙有闲,经历了一次病痛的洗礼,才认识到了只有好好的生活,认真的对待自己的身体,才能更好的去工作,没有了身体的支撑,什么工作都只能是纸老虎,不攻自破.在这里也祝愿大家,在生活中好 ...

  7. PHP快速排序及其时间复杂度

    <?php function quickSort(&$arr, $l, $r) { if (count($arr)<2 || $l>$r) return; $tmp_l = ...

  8. hibernate反向工程 (eclipse和myeclipse)(转)

    hibernate反向工程 (eclipse和myeclipse) 如何提取数据库的模式信息,想通过hibernate的反向工具尝试下. 一.myeclipse下hibernate反向工程: 1.选择 ...

  9. 在EntityFramework中使用 nock的方法。

    以下内容为转载: A:https://dotblogs.com.tw/asdtey/2009/09/27/10793 B:http://www.gitshah.com/2014/08/how-to-a ...

  10. OpenJudge计算概论-Tomorrow never knows【输入日期计算下一天的日期】

    /*====================================================================== Tomorrow never knows? 总时间限制 ...