結果

問題 No.874 正規表現間距離
ユーザー tnakao0123tnakao0123
提出日時 2019-09-05 12:58:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,884 bytes
コンパイル時間 562 ms
コンパイル使用メモリ 85,360 KB
実行使用メモリ 19,416 KB
最終ジャッジ日時 2023-09-02 00:33:22
合計ジャッジ時間 3,651 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 5 ms
7,552 KB
testcase_17 AC 5 ms
7,332 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 12 ms
11,700 KB
testcase_26 AC 12 ms
11,676 KB
testcase_27 AC 30 ms
19,156 KB
testcase_28 WA -
testcase_29 AC 34 ms
19,208 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1 ms
4,376 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 2 ms
4,380 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 && ac == bc) {
	setmin(dp[i + 1][j + 1], dp[i][j]);
	if (am == '*')
	  setmin(dp[i][j + 1], dp[i][j]);
	if (bm == '*')
	  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