2016 Multi-University Training Contest 5 Two
本文转自: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的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 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 ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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, ...
随机推荐
- URAL 1085 Meeting(最短路)
Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- 【转】详解使用tcpdump、wireshark对Android应用程序进行抓包并分析
原文网址:http://blog.csdn.net/gebitan505/article/details/19044857 本文主要介绍如何使用tcpdump和wireshark对Android应用程 ...
- 【转贴】Cortex系列M0-4简单对比
转载网址:http://blog.sina.com.cn/s/blog_7dbd9c0e01018e4l.html 最近搞了块ST的Cortex-M4处理器,然后下了本文档.分享一下. 针对目前进入大 ...
- feature visualization from ipython notebook
Feature visualization from ipython notebook Wang Xiao 1. install anaconda2 from: https://www.continu ...
- php mcrypt 完全安装
今天安装完 PHP ,访问某个功能时, /var/log/httpd/error_log 中报如下错误: PHP Fatal error: Call to undefined function ...
- lucene 基本原理整理
基本原理:http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623594.html 所有过程:http://www.cnblogs.co ...
- pgbouncer配置
DESCRIPTION pgbouncer is a PostgreSQL connection pooler. Any target application can be connected to ...
- 开发成功-cpu-mem监控动态折线图--dom esayui js java
jsp ------------------------------------------------------------------------------------------- ---- ...
- generator函数
function* helloWordGenerator() { yield "hello"; yield "world"; return "endi ...