結果

問題 No.874 正規表現間距離
ユーザー risujirohrisujiroh
提出日時 2019-08-30 22:56:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 1,872 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 173,376 KB
実行使用メモリ 50,728 KB
最終ジャッジ日時 2023-08-14 07:11:04
合計ジャッジ時間 4,190 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 8 ms
7,304 KB
testcase_17 AC 8 ms
7,224 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 28 ms
19,064 KB
testcase_26 AC 24 ms
19,076 KB
testcase_27 AC 71 ms
50,504 KB
testcase_28 AC 52 ms
36,508 KB
testcase_29 AC 70 ms
50,728 KB
testcase_30 AC 69 ms
50,636 KB
testcase_31 AC 59 ms
42,524 KB
testcase_32 AC 60 ms
42,560 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 18 ms
13,644 KB
testcase_37 AC 17 ms
13,572 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string A, B; cin >> A >> B;
  string s, t;
  V<> a, b;
  for (int i = 0; i < (int)A.size(); ++i) {
    s += A[i];
    if (i + 1 < (int)A.size() and !islower(A[i + 1])) {
      a.push_back(A[++i] == '?' ? 1 : 2);
    } else {
      a.push_back(0);
    }
  }
  for (int i = 0; i < (int)B.size(); ++i) {
    t += B[i];
    if (i + 1 < (int)B.size() and !islower(B[i + 1])) {
      b.push_back(B[++i] == '?' ? 1 : 2);
    } else {
      b.push_back(0);
    }
  }
  int n = a.size(), m = b.size();

  VV<> c(26, V<>(n + 1)), d(26, V<>(m + 1));
  for (int x = 0; x < 26; ++x) {
    for (int i = n - 1; i >= 0; --i) {
      c[x][i] = (s[i] - 'a' != x) + c[x][i + 1];
    }
    for (int j = m - 1; j >= 0; --j) {
      d[x][j] = (t[j] - 'a' != x) + d[x][j + 1];
    }
  }

  VV<> dp(n + 1, V<>(m + 1, 1e9));
  VV<> p(n + 1, V<>(m + 1, 1e9));
  VV<> q(n + 1, V<>(m + 1, 1e9));
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) {
    if (i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + !a[i - 1]);
    if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + !b[j - 1]);
    if (i and j) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (s[i - 1] != t[j - 1]));
      if (a[i - 1] == 2) {
        int x = s[i - 1] - 'a';
        dp[i][j] = min(dp[i][j], p[i - 1][j] - d[x][j]);
      }
      if (b[j - 1] == 2) {
        int y = t[j - 1] - 'a';
        dp[i][j] = min(dp[i][j], q[i][j - 1] - c[y][i]);
      }
    }
    if (i < n and j < m) {
      int x = s[i] - 'a';
      p[i][j + 1] = min(p[i][j], dp[i][j] + d[x][j]);
      int y = t[j] - 'a';
      q[i + 1][j] = min(q[i][j], dp[i][j] + c[y][i]);
    }
  }
  cout << dp[n][m] << '\n';
}
0