結果

問題 No.874 正規表現間距離
ユーザー koba-e964koba-e964
提出日時 2021-11-04 22:51:53
言語 Rust
(1.77.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 2,551 bytes
コンパイル時間 3,078 ms
コンパイル使用メモリ 166,848 KB
実行使用メモリ 33,280 KB
最終ジャッジ日時 2024-04-23 05:40:03
合計ジャッジ時間 2,140 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 0 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 0 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 5 ms
6,944 KB
testcase_17 AC 5 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 19 ms
12,288 KB
testcase_26 AC 19 ms
12,416 KB
testcase_27 AC 53 ms
33,280 KB
testcase_28 AC 39 ms
23,936 KB
testcase_29 AC 55 ms
33,280 KB
testcase_30 AC 54 ms
33,280 KB
testcase_31 AC 46 ms
27,776 KB
testcase_32 AC 46 ms
27,904 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 1 ms
6,944 KB
testcase_36 AC 13 ms
8,704 KB
testcase_37 AC 12 ms
8,832 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 1 ms
6,944 KB
testcase_41 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

fn compile(a: &[char]) -> Vec<(char, u8)> {
    let mut ans: Vec<(char, u8)> = vec![];
    for &c in a {
        let l = ans.len();
        if c == '?' {
            ans[l - 1].1 = 1;
        } else if c == '*' {
            ans[l - 1].1 = 2;
        } else {
            ans.push((c, 0));
        }
    }
    ans
}

fn main() {
    let a: Vec<_> = getline().trim().chars().collect();
    let b: Vec<_> = getline().trim().chars().collect();
    let a = compile(&a);
    let b = compile(&b);
    let n = a.len();
    let m = b.len();
    const INF: i64 = 1 << 40;
    let mut dp = vec![vec![INF; m + 1]; n + 1];
    dp[0][0] = 0;
    for i in 0..n + 1 {
        let (xc, xk) = if i < n { a[i] } else { ('+', 0) };
        for j in 0..m + 1 {
            let val = dp[i][j];
            let (yc, yk) = if j < m { b[j] } else { ('-', 0) };
            if i < n && j < m {
                if xc == yc || (xk != 0 && yk != 0) {
                    dp[i + 1][j + 1] = min(dp[i + 1][j + 1], val);
                } else {
                    dp[i + 1][j + 1] = min(dp[i + 1][j + 1], val + 1);
                }
            }
            if j < m {
                let add = if yk != 0 || (xc == yc && xk == 2) {
                    0
                } else {
                    1
                };
                dp[i][j + 1] = min(dp[i][j + 1], val + add);
            }
            if i < n {
                let add = if xk != 0 || (xc == yc && yk == 2) {
                    0
                } else {
                    1
                };
                dp[i + 1][j] = min(dp[i + 1][j], val + add);
            }
        }
    }
    println!("{}", dp[n][m]);
}
0