結果

問題 No.874 正規表現間距離
ユーザー risujirohrisujiroh
提出日時 2019-08-30 22:37:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,737 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 174,804 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-11-22 01:15:00
合計ジャッジ時間 11,900 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 36 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

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