結果

問題 No.874 正規表現間距離
ユーザー risujirohrisujiroh
提出日時 2019-08-30 22:37:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,737 bytes
コンパイル時間 2,128 ms
コンパイル使用メモリ 174,900 KB
実行使用メモリ 21,664 KB
最終ジャッジ日時 2024-05-01 19:09:50
合計ジャッジ時間 9,637 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 256 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 TLE -
testcase_26 AC 13 ms
8,832 KB
testcase_27 AC 36 ms
19,456 KB
testcase_28 TLE -
testcase_29 AC 35 ms
19,456 KB
testcase_30 AC 36 ms
19,200 KB
testcase_31 AC 1,071 ms
16,640 KB
testcase_32 AC 1,064 ms
16,640 KB
testcase_33 AC 5 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 3 ms
6,944 KB
testcase_36 AC 68 ms
6,940 KB
testcase_37 AC 56 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 3 ms
6,940 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));
  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) {
        for (int k = 0; k < j; ++k) {
          int x = s[i - 1] - 'a';
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + d[x][k] - d[x][j]);
        }
      }
      if (b[j - 1] == 2) {
        for (int k = 0; k < i; ++k) {
          int x = t[j - 1] - 'a';
          dp[i][j] = min(dp[i][j], dp[k][j - 1] + c[x][k] - c[x][i]);
        }
      }
    }
  }
  cout << dp[n][m] << '\n';
}
0