跳转至

双序列 DP

最长公共子序列

AtCoder dp_f

最长公共子序列,longest common subsequence,简称 LCS。

题意

1
给定两个字符串 $s, t$,求出它们的最长公共子序列 $x$,即 $x$ 既是 $s$ 的子序列又是 $t$ 的子序列。$1 \le |s|, |t| \le 3000$。

思路

可以考虑如何构造公共的子序列。一种容易想到的思路是,先搜索出 \(s\) 的子序列 \(a\),再判断 \(a\) 是否为 \(t\) 的子序列,如果是,记录最长的 \(a\)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 先求出 s 的子序列,再判该子序列是否为 t 的子序列
#include <bits/stdc++.h>

using namespace std;

string s, t;
string ans;

// 判断 a 是 b 的子序列
bool Check(const string &a, const string &b) {
  int n = a.size(), m = b.size(), i, j;
  for (i = 0, j = 0; i < n && j < m; j++) {
    i += a[i] == b[j];
  }
  return i == n;
}

void dfs(int last, string res) {
  if (!Check(res, t)) {
    return ;
  }
  if (res.size() > ans.size()) {
    ans = res;
  }
  for (int i = last + 1; i < s.size(); i++) {
    dfs(i, res + s[i]);
  }
}

int main() {
  cin >> s >> t;
  dfs(-1, "");
    cout << ans;
  return 0;
}

实际上,还有另外一种比较简单的直接构造公共子序列的方法。我们定义两个指针 \(i, j\),用于指向 \(s\) 和 \(t\) 的字符,一开始它们为 \(0\),指向 \(s\) 和 \(t\) 的首字符。然后我们可以按照如下三条规则构造公共子序列:

  1. \(s_i\) 不在公共子序列中,此时 \(i\) 加 \(1\)(\(i \le |s|\))。
  2. \(t_j\) 不在公共子序列中,此时 \(j\) 加 \(1\)(\(j \le |t|\))。
  3. \(s_i = t_j\) 时,可以将 \(s_i\)(或 \(t_j\))加入到结果的公共子序列中(\(i \le |s|, j \le |t|\)),也可以不放入。

状态和转移

先求解 LCS 的长度。

状态:\((i, j, l)\) 表示 \(s_1 \sim s_i\) 和 \(t_1 \sim t_j\) 的公共子序列的长度为 \(l\)。

转移:

  • 当 \(s_i \ne t_j\) 时,\((i, j, l) \to (i + 1, j, l), (i, j + 1, l)\)
  • 当 \(s_i = t_j\) 时,\((i, j, l) \to (i + 1, j + 1, l), (i + 1, j + 1, l + 1)\)

初始状态:\((1, 1, [s_1 = t_1])\)。

目标状态:所有能够搜索到的状态 \((i, j, l)\) 中 \(l\) 的最大值。

无后效性:以不同的转移路径到达同一组状态 \((i, j, l)\),其后续的转移是一致的。

最优子结构性质:

  • 指定 \(l\) 为最大化属性。
  • 拓扑序为 \(i\) 从小到大,\(i\) 相同时 \(j\) 从小到大。
  • 对于同一组 \((i, j)\),只保留 \(l\) 最大的最优状态,因为非最优状态不能转移到拓扑序中靠后的最优状态。

由于 \(l\) 是最大化属性,因此当 \(s_i = t_j\) 时的转移 \((i, j, l) \to (i + 1, j + 1, l)\) 一定不是最优的转移。

DP

状态:\(dp_{i, j}\) 表示 \(s_1 s_2 \dots s_i\) 和 \(t_1 t_2 \dots t_j\) 的公共子序列的最长长度。

收集型转移:

  • 当 \(s_i = t_j\) 时,\(dp_{i, j} = dp_{i - 1, j - 1} + 1\)。
  • 否则,\(dp_{i, j} = \max \{ dp_{i - 1, j}, dp_{i, j - 1} \}\)。

初始:全部 \(dp_{i, j}\) 初始化为 \(0\)。

目标:\(dp_{|s|, |t|}\)。

细节:\(i, j\) 的下标从 \(1\) 开始。

按照 \(i\) 从 \(1\) 到 \(|s|\)、\(j\) 从 \(1\) 到 \(|t|\) 的顺序 DP 求解。

时空复杂度

记录 \(O(|s| \times |t|)\) 个状态,空间复杂度 \(O(|s| \times |t|)\)。

每个状态至多转移两次,时间复杂度 \(O(|s| \times |t|)\)。

方案输出

使用状态关联,从目标状态开始判断是否为最优转移,寻找最优方案。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>

using namespace std;

const int N = 3e3 + 5;

string s, t, ans;
int dp[N][N];
int n, m;

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> s >> t;
  n = s.size(), m = t.size();
  s = ' ' + s, t = ' ' + t;  // 字符串下标从 1 开始
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  for (int i = n, j = m; i >= 1 && j >= 1;) {
    if (s[i] == t[j]) {
      ans += s[i];
      i--, j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}

寻找方案的时间复杂度为 \(O(|s| + |t|)\)。

编辑距离

洛谷 P2758 CSES 1639

题意

给定字符串 \(s\) 和 \(t\),你可以在字符串 \(s\) 中插入、删除、修改字符,求出使得 \(s\) 变为 \(t\) 的最少操作次数。\(1 \le |s|, |t| \le 5000\)。

思路

同样地,考虑如何构造修改方案。正着直接构造方案有点困难,我们不妨利用分治的思想将大问题分解成小问题进行求解。

令 \(F(i, j)\) 表示将 \(s_1 s_2 \dots, s_i\) 变为 \(t_1 t_2 \dots t_j\) 的最少修改次数,则原问题为 \(F(|s|, |t|)\)。

对于问题 \(F(i, j)\),我们可以将其做如下分解:

  1. 当 \(s_i = t_j\) 时,将问题分解为 \(F(i - 1, j - 1)\)。
  2. 当 \(s_i \ne t_j\) 时,可以考虑在字符 \(s_i\) 之后插入一个字符 \(t_j\),将问题分解为 \(F(i, j - 1) + 1\)。
  3. 当 \(s_i \ne t_j\) 时,可以考虑直接将字符 \(s_i\) 直接删掉,将问题分解为 \(F(i - 1, j) + 1\)。
  4. 当 \(s_i \ne t_j\) 时,可以考虑将字符 \(s_i\) 修改为 \(t_j\),将问题分解为 \(F(i - 1, j - 1) + 1\)。

拓扑序为 \(i\) 从小到大,\(i\) 相同时 \(j\) 从小到大。

DP

状态:\(dp_{i, j}\) 表示将 \(s_1 \sim s_i\) 变为 \(t_1 \sim t_j\) 的最少修改次数。

收集型转移:

  • 当 \(s_i = t_j\) 时,\(dp_{i, j} = dp_{i - 1, j - 1}\)。
  • 否则,\(dp_{i, j} = \min \{ dp_{i - 1, j}, dp_{i, j - 1}, dp_{i - 1, j - 1} \} + 1\)。

初始:\(dp_{0, j} = j, dp_{i, 0} = i\),其中 \(1 \le i \le |s|, 1 \le j \le |t|\)。

目标:\(dp_{|s|, |t|}\)。

按照 \(i\) 从 \(1\) 到 \(|s|\)、\(j\) 从 \(1\) 到 \(|t|\) 的顺序 DP 求解。

时空复杂度

记录 \(O(|s| \times |t|)\) 个状态,空间复杂度为 \(O(|s| \times |t|)\)。

每个状态至多转移 \(3\) 次,时间复杂度为 \(O(|s| \times |t|)\)。

实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5e3 + 1;

string s, t;
int n, m, dp[MAXN][MAXN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> s >> t;
  n = s.size(), m = t.size();
  s = '#' + s, t = '#' + t;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = i;
  }
  for (int i = 1; i <= m; i++) {
    dp[0][i] = i;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1;
      }
    }
  }
  cout << dp[n][m];
  return 0;
}