結果

問題 No.874 正規表現間距離
ユーザー risujirohrisujiroh
提出日時 2019-08-30 22:51:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,891 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 172,612 KB
実行使用メモリ 23,688 KB
最終ジャッジ日時 2023-08-14 07:00:13
合計ジャッジ時間 7,330 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,504 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 447 ms
7,276 KB
testcase_17 AC 459 ms
7,508 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

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) {
        dp[i][j] = min(dp[i][j], p[i - 1][j]);
      }
      if (b[j - 1] == 2) {
        dp[i][j] = min(dp[i][j], q[i][j - 1]);
      }
    }
    if (i < n) for (int nj = j + 1; nj <= m; ++nj) {
      int x = s[i] - 'a';
      p[i][nj] = min(p[i][nj], dp[i][j] + d[x][j] - d[x][nj]);
    }
    if (j < m) for (int ni = i + 1; ni <= n; ++ni) {
      int x = t[j] - 'a';
      q[ni][j] = min(q[ni][j], dp[i][j] + c[x][i] - c[x][ni]);
    }
  }
  cout << dp[n][m] << '\n';
}
0