本文转自: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. POJ2112 Optimal Milking (网络流)(Dinic)

                                             Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  2. Linux 命令ln

    在linux中可用ln命令创建一个文件的链接(软链接或者硬链接) 硬链接的使用: root@IdeaPad:~# ln 2.txt e.txt root@IdeaPad:~# ls 1.txt 2.t ...

  3. Linux驱动设计—— 内外存访问

    本节对内外存访问做详细的介绍. 驱动程序加载成功的一个关键因素,就是内核能够为驱动程序分配足够的内存空间.这些空间一部分用于驱动程序必要的数据结构,另一部分用于数据的交换.同时,内核也应该具有访问外部 ...

  4. 黑马程序员——JAVA基础之简述多线程,两种创建多线程的方式

    ------- android培训.java培训.期待与您交流! ---------- 多线程: 进程和线程: 进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者 ...

  5. 《FreeSWITCH: VoIP实战》:SIP 模块 - mod_sofia

    SIP 模块是 FreeSWITCH 的主要模块,所以,值得拿出专门一章来讲解. 在前几章时里,你肯定见过几次 sofia 这个词,只是或许还不知道是什么意思.是这样的,Sofia-SIP 是由诺基亚 ...

  6. windows API

    Process and Thread Functions https://msdn.microsoft.com/en-us/library/windows/desktop/ms684847(v=vs. ...

  7. Freezing Your Tuples Off 之 vacuum_freeze_min_age

    The vacuum_freeze_min_age setting determines the youngest XID which will be changed to FrozenXID on ...

  8. python字符串关键点总结

    python字符串关键点有下面几点: 1.一些引号分隔的字符 你可以把字符串看出是Python的一种数据类型,在Python单引号或者双引号之间的字符数组或者连续的字符集合.在python中最常用的引 ...

  9. share point 2013 显示详细错误信息?

    Wednesday, April 6, 2011 at 17:40 |  Post a Comment SharePoint "Unknown Error": How to Sho ...

  10. 【转】C#安装包(自动卸载低版本)

    一:版本号升级 1.ProductCode.Version(version比前一版本要高比如以前1.0.0现在就是1.0.1),     2.保持UpgradeCode不变(当设置RemovePrev ...