結果

問題 No.874 正規表現間距離
ユーザー lumc_lumc_
提出日時 2019-08-30 22:24:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 5,477 bytes
コンパイル時間 1,002 ms
コンパイル使用メモリ 115,036 KB
実行使用メモリ 34,812 KB
最終ジャッジ日時 2024-11-22 00:31:37
合計ジャッジ時間 3,092 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 1 ms
6,820 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 3 ms
6,816 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 23 ms
16,668 KB
testcase_17 AC 32 ms
16,904 KB
testcase_18 AC 2 ms
6,820 KB
testcase_19 AC 1 ms
6,816 KB
testcase_20 AC 2 ms
6,816 KB
testcase_21 AC 2 ms
6,820 KB
testcase_22 AC 2 ms
6,820 KB
testcase_23 AC 2 ms
6,820 KB
testcase_24 AC 2 ms
6,820 KB
testcase_25 AC 119 ms
34,624 KB
testcase_26 AC 79 ms
34,740 KB
testcase_27 AC 88 ms
34,768 KB
testcase_28 AC 96 ms
34,812 KB
testcase_29 AC 88 ms
34,788 KB
testcase_30 AC 87 ms
34,688 KB
testcase_31 AC 94 ms
34,624 KB
testcase_32 AC 94 ms
34,688 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 3 ms
5,248 KB
testcase_36 AC 26 ms
15,360 KB
testcase_37 AC 26 ms
15,360 KB
testcase_38 AC 2 ms
5,248 KB
testcase_39 AC 2 ms
5,248 KB
testcase_40 AC 3 ms
5,248 KB
testcase_41 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template < int n, class... T >
typename std::enable_if< (n >= sizeof...(T)) >::type __output_tuple(
    std::ostream &, std::tuple< T... > const &) {}
template < int n, class... T >
typename std::enable_if< (n < sizeof...(T)) >::type __output_tuple(
    std::ostream &os, std::tuple< T... > const &t) {
  os << (n == 0 ? "" : ", ") << std::get< n >(t);
  __output_tuple< n + 1 >(os, t);
}
template < class... T >
std::ostream &operator<<(std::ostream &os, std::tuple< T... > const &t) {
  os << "(";
  __output_tuple< 0 >(os, t);
  os << ")";
  return os;
}
template < class T, class U >
std::ostream &operator<<(std::ostream &os, std::pair< T, U > const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template < class T >
std::ostream &operator<<(std::ostream &os, const std::stack< T > &a) {
  os << "{";
  for(auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template < class T, class Container, class Compare >
std::ostream &operator<<(std::ostream &os,
    std::priority_queue< T, Container, Compare > a) {
  os << "{ (top) ";
  while(a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template < class T, class Container >
std::ostream &operator<<(std::ostream &os, std::queue< T, Container > a) {
  os << "{ ";
  while(a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
#define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                                \
  [&]() {                                                                        \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                             \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " << __debug_tap \
    << std::endl;                                                      \
  }()
template < class T >
inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for(size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template < class T >
inline void dump1D(T &d, size_t sizey) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        os << "{";
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : ", ") << *ite;
        os << "}";
        return os;
      }
#else
#define dump(...) ((void) 42)
#define dump2D(...) ((void) 42)
#define dump1D(...) ((void) 42)
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : " ") << *ite;
        return os;
      }
#endif
// }}}


ll dp[2002][2002];

constexpr ll inf = 1e9;
template <class T, class U> inline void smax(T &a, U b) { a = a > b ? a : b; }
template <class T, class U> inline void smin(T &a, U b) { a = a < b ? a : b; }


int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  dump(n, m);

  s += "__";
  t += "__";

  for(int i = 0; i <= n; i++) for(int j = 0; j <= m; j++) dp[i][j] = inf;

  dp[0][0] = 0;

  for(int i = 0; i <= n; i++) for(int j = 0; j <= m; j++) {
    char c1 = s[i], c2 = t[j];
    if(c1 == '?' or c1 == '*') continue;
    if(c2 == '?' or c2 == '*') continue;
    int f1 = s[i+1] == '?' ? 1 : s[i+1] == '*' ? 2 : 0;
    int f2 = t[j+1] == '?' ? 1 : t[j+1] == '*' ? 2 : 0;

    for(int k = 0; k < 2; k++) for(int l = 0; l < 2; l++) {
      if(k == 1 and !f1) continue;
      if(l == 1 and !f2) continue;
      char z1 = c1, z2 = c2;
      if(k) z1 = 0;
      if(l) z2 = 0;
      if(c1 == '_') z1 = 0;
      if(c2 == '_') z2 = 0;
      for(int u = 0; u < 2; u++) for(int v = 0; v < 2; v++) {
        if(u == 0 and v == 0) continue;
        if(u == 0 and f1 != 2) continue;
        if(v == 0 and f2 != 2) continue;
        int ni = i+((!!f1)+1)*u;
        int nj = j+((!!f2)+1)*v;
        if(ni <= n and nj <= m) smin(dp[ni][nj], dp[i][j] + (z1 != z2));
        if(v and nj <= m) smin(dp[i][nj], dp[i][j] + (z2 != 0));
        if(u and ni <= n) smin(dp[ni][j], dp[i][j] + (z1 != 0));
      }
    }
    // dump(i, j, dp[i][j]);
  }
  
  cout << dp[n][m] << endl;
  return 0;
}
0