题目链接: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. c 语言函数分析

    第一个参数为指向线程标识符的指针. 第二个参数用来设置线程属性. 第三个参数是线程运行函数的起始地址. 最后一个参数是运行函数的参数. result = pthread_create(&tid ...

  2. vue-为什么子组件中的data选项必须是函数?

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. delphi Sqlite

    Delphi中SQLite如何读写二进制字段(Blob类型) 在Delphi中,有大量的组件可以操作SQLite数据库,如UniDAC就是其中一个比较优秀的,当然还有ASQLite3Component ...

  4. 解决Oracle迁移至GreenPlum(PostgreSql)后不支持rownum的问题

    只需在查询结果中添加一列 row_number() over() as rownum即可 如: select row_number() over() as rownum , * from table1 ...

  5. C#-Api:身份证识别

    ylbtech-C#-Api:身份证识别 通过POST上传base64格式的图片内容,可识别二代身份证.驾照.行驶证.军官证.中华人民共和国往来港澳通行证.台湾居民往来大陆通行证.大陆居民往来台湾通行 ...

  6. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  7. java could not open `C|D|E|F:\jre\lib\amd64\jvm.cfg' 解决方案与原因

    因为安装了 jdk 后发现有多个 jre 一个是安装目录下的. 还有一个是安装后的自动安装的注意路径都不一样. 由于本人有强迫症所有不能容忍有两个 jre 目录的存在,所以果断删除了 D 盘下的.谨慎 ...

  8. -bash: make: command not found

    yum -y install gcc automake autoconf libtool make

  9. java 传入多个参数时报"Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1,..." 解决方案

    @Select("SELECT id FROM ae_post ORDER BY id DESC LIMIT #{page},#{size}") List<Post> ...

  10. 学习k8s的经验

    最近在学k8s,总结一下安装k8s的坑. 1.晚上关于k8s的学习资料很多,多不如精,这个博客很好,https://blog.csdn.net/sinat_35930259/article/categ ...