結果

問題 No.874 正規表現間距離
ユーザー lam6er
提出日時 2025-04-16 00:09:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,452 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 131,908 KB
最終ジャッジ日時 2025-04-16 00:10:45
合計ジャッジ時間 3,820 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 3
other AC * 15 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

def parse_pattern(s):
    patterns = []
    i = 0
    while i < len(s):
        if i + 1 < len(s) and s[i+1] in ['?', '*']:
            patterns.append((s[i], s[i+1]))
            i += 2
        else:
            patterns.append((s[i], ''))
            i += 1
    return patterns

A = input().strip()
B = input().strip()

A_pat = parse_pattern(A)
B_pat = parse_pattern(B)

n = len(A_pat)
m = len(B_pat)

INF = float('inf')
dp = [[INF] * (m + 1) for _ in range(n + 1)]
dp[0][0] = 0

for i in range(n + 1):
    for j in range(m + 1):
        if dp[i][j] == INF:
            continue
        
        # Transition 1: Skip A's current pattern if possible
        if i < n:
            a_char, a_opt = A_pat[i] if i < n else ('', '')
            if a_opt in ['?', '*']:
                if dp[i+1][j] > dp[i][j]:
                    dp[i+1][j] = dp[i][j]
        
        # Transition 2: Skip B's current pattern if possible
        if j < m:
            b_char, b_opt = B_pat[j] if j < m else ('', '')
            if b_opt in ['?', '*']:
                if dp[i][j+1] > dp[i][j]:
                    dp[i][j+1] = dp[i][j]
        
        # Transition 3: Generate one character from both patterns
        if i < n and j < m:
            a_char, a_opt = A_pat[i]
            b_char, b_opt = B_pat[j]
            cost = 0 if a_char == b_char else 1
            if dp[i+1][j+1] > dp[i][j] + cost:
                dp[i+1][j+1] = dp[i][j] + cost

print(dp[n][m])
0