本文转自: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. POJ-2886 Who Gets the Most Candies?(线段树+模拟)

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

  2. C++虚函数、赋值兼容原则

    #include <iostream.h> class A { public: void f1() { cout << "a" << endl; ...

  3. Python元组的简单介绍

    1.实际上元组是跟列表非常相近的另一种容器类型.元组和列表看上去的不同的一点是元组用圆括号而列表用方括号.而在功能上,元组是一种不可变的类型.正是因为这个原因,元组可以做一些列表不可以做的事情,比如用 ...

  4. PHP正则匹配邮件地址、URL

    匹配邮件 #^[A-Za-z0-9]+[\w\.-]*@[A-Za-z0-9]+[A-Za-z0-9\.-]*[A-Za-z0-9]$# 注意 1. \w 表示的是 [A-Za-z0-9_] 包括下划 ...

  5. 当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误

    当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示. 有两种方法可以解决以上问题: 1.修改we ...

  6. 003. 连接access数据库代码

    1. 前端aspx中的代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="De ...

  7. OpenJudge计算概论-整数奇偶排序

    /*===================================== 整数奇偶排序 总时间限制: 1000ms 内存限制: 65536kB 描述 输入10个整数,彼此以空格分隔 重新排序以后 ...

  8. es6语法重构react代码

    1.使用React.Component创建组件,需要通过在constructor中调用super()将props传递给React.Component.另外react 0.13之后props必须是不可变 ...

  9. Java并发之CopyOnWriteArrayList

    CopyOnWriteArrayList是线程安全的.并且读操作无锁的ArrayList.不像ArrayList默认初始化大小为10的Object[],CopyOnWriteArrayList默认初始 ...

  10. spring配置中,properties文件以及xml文件配置问题

    spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...