题目链接:https://atcoder.jp/contests/abc130/tasks/abc130_e

题目大意

  给定一个长度为 N 的序列 S 和一个长度为 M 的序列 T,问 S 和 T 中有多少对不相同的公共子序列?

  PS:两个子序列对只有同时满足内容完全相同和在 S 和 T 中的位置完全相同才算相同。

分析

  设 S(i) 表示 S 序列的前 i 项,设 T(j) 表示 T 序列的前 j 项,dp[i][j] 表示 S(i) 和 T(j) 中有多少对不相同的公共子序列。
  首先 dp[i - 1][j],dp[i][j - 1], dp[i - 1][j - 1] 有的,dp[i][j] 都有,因此 dp[i][j] 至少是 dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]。
  区别在于 S[i] 和 T[j],如果它们相等,那么 dp[i][j] 就要加上 dp[i - 1][j - 1],相当于把 dp[i - 1][j - 1] 中的所有对公共子序列都延长了 1;否则不变。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 2e3 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int N, M, S[maxN], T[maxN];
// dp[i][j]表示 S 的前 i 项和 T 的前 j 项有多少相同的子序列对
LL dp[maxN][maxN]; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
cin >> N >> M;
For(i, , N) cin >> S[i];
For(i, , M) cin >> T[i]; // 初始化(空集算一个)
For(i, , N) dp[i][] = ;
For(j, , M) dp[][j] = ; For(i, , N) {
For(j, , M) {
// 容斥原理
dp[i][j] = dp[i - ][j] + dp[i][j - ] - dp[i - ][j - ];
if(S[i] == T[j]) dp[i][j] += dp[i - ][j - ];
dp[i][j] = (dp[i][j] + mod) % mod;
}
} cout << dp[N][M] << endl;
return ;
}

AtCoder ABC 130E Common Subsequence的更多相关文章

  1. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  2. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

  3. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  4. LeetCode 1143. Longest Common Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and te ...

  5. Leetcode: Longest Common Subsequence

    Given two strings text1 and text2, return the length of their longest common subsequence. A subseque ...

  6. Longest Common Subsequence (DP)

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  7. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  8. leetcode1143 Longest Common Subsequence

    """ Given two strings text1 and text2, return the length of their longest common subs ...

  9. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

随机推荐

  1. [转]在WPF中自定义控件 UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  2. nodejs的npm修改源

    在国内用npm install时,速度总是不尽如人意,解决办法是修改npm的数据源 npm config set registry https://registry.npm.taobao.org 修改 ...

  3. BASS HOME

     http://www.un4seen.com/

  4. delphi RichView的使用介绍

    RichView 组件 由 9 个组件模块组成,分别是: 1.TRVStyle:主要是定义RICHVIEW样式,定义后,其它RIHCVIEW都可以引用此样式.  2.TRichView :主要用于显示 ...

  5. GlobalExceptionHandler 捕获抛出的异常,返回特定值给前端

    /** * @author hhh * @date 2019/1/17 16:28 * @Despriction */ @ResponseBody @ControllerAdvice @Log4j2 ...

  6. 揭秘!2周实现上云上市,阿里云SaaS上云工具包如何打造新云梯?

    提到“上云”,很多人会理解成上IaaS,比如买一些计算.存储和网络云产品,把自己的应用系统部署上去.这的确是通常意义的上云.但对SaaS而言,需要从产品.商业.服务,三个维度考虑SaaS伙伴和客户的痛 ...

  7. 好久没写题解了= =这次是bzoj 1051

    唉= =这道题我都想到了tarjan缩点,但是没有想到最后一步啊= =我们很容易想到反向建边然后缩点,这时候我们看由多少个联通块的入度为0,如果为1个,那就输出这个块的大小,否则输出0: #inclu ...

  8. thinkphp 标签扩展

    标签库加载直线电机参数 模板中加载标签库,预加载自定义标签库,扩展内置标签库的加载 请参考:http://document.thinkphp.cn/manual_3_2.html#taglib 自定义 ...

  9. thinkphp 数据库缓存

    默认的数据库驱动位于Think\Db\Driver命名空间下面,驱动类必须继承Think\Db类,每个数据库驱动必须要实现的接口方法包括(具体参数可以参考现有的数据库驱动类库): 驱动方法 方法说明 ...

  10. USACO 06JAN 牛的舞会 洛谷2863

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...