結果

問題 No.874 正規表現間距離
ユーザー tnakao0123tnakao0123
提出日時 2019-09-05 13:02:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,919 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 84,348 KB
実行使用メモリ 19,252 KB
最終ジャッジ日時 2023-09-02 00:38:03
合計ジャッジ時間 3,092 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 4 ms
7,328 KB
testcase_17 AC 4 ms
7,276 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 11 ms
11,732 KB
testcase_26 AC 10 ms
11,680 KB
testcase_27 AC 25 ms
19,252 KB
testcase_28 AC 27 ms
17,812 KB
testcase_29 AC 25 ms
19,176 KB
testcase_30 AC 30 ms
19,184 KB
testcase_31 AC 29 ms
18,124 KB
testcase_32 AC 29 ms
17,868 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,504 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 10 ms
11,708 KB
testcase_37 AC 10 ms
11,728 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 874.cc:  No.874 正規表現間距離 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 2000;
const int INF = 1 << 30;

/* typedef */

typedef pair<char,char> pcc;

/* global variables */

char sa[MAX_N + 4], sb[MAX_N + 4];
pcc as[MAX_N + 1], bs[MAX_N + 1];
int dp[MAX_N + 1][MAX_N + 1];

/* subroutines */

int parse(char s[], pcc v[]) {
  int n = 0;
  for (int i = 0; s[i]; i++) {
    if (s[i] == '?') v[n - 1].second = '?';
    else if (s[i] == '*') v[n - 1].second = '*';
    else v[n++] = pcc(s[i], '.');
  }
  return n;
}

void printpcc(int n, pcc v[]) {
  for (int i = 0; i < n; i++) {
    putchar(v[i].first);
    putchar(v[i].second);
  }
  putchar('\n');
}

inline void setmin(int &a, int b) { if (a > b) a = b; }

/* main */

int main() {
  scanf("%s%s", sa, sb);
  int an = parse(sa, as);
  int bn = parse(sb, bs);
  //printpcc(an, as); printpcc(bn, bs);

  for (int i = 0; i <= an; i++)
    fill(dp[i], dp[i] + bn + 1, INF);
  dp[0][0] = 0;

  for (int i = 0; i <= an; i++) {
    char &ac = as[i].first, &am = as[i].second;

    for (int j = 0; j <= bn; j++) {
      char &bc = bs[j].first, &bm = bs[j].second;

      if (i < an && j < bn) {
	setmin(dp[i + 1][j + 1], dp[i][j] + ((ac != bc) ? 1 : 0));
	if (am == '*' && ac == bc)
	  setmin(dp[i][j + 1], dp[i][j]);
	if (bm == '*' && ac == bc)
	  setmin(dp[i + 1][j], dp[i][j]);
      }

      if (i < an)
	setmin(dp[i + 1][j], dp[i][j] + ((am == '.') ? 1 : 0));
      if (j < bn)
	setmin(dp[i][j + 1], dp[i][j] + ((bm == '.') ? 1 : 0));
    }
  }

  printf("%d\n", dp[an][bn]);
  return 0;
}
0