結果

問題 No.874 正規表現間距離
ユーザー akakimidoriakakimidori
提出日時 2019-08-31 08:06:42
言語 Rust
(1.77.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 2,982 bytes
コンパイル時間 15,007 ms
コンパイル使用メモリ 377,216 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-05-03 03:56:56
合計ジャッジ時間 15,742 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 6 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 21 ms
7,168 KB
testcase_26 AC 19 ms
7,296 KB
testcase_27 AC 39 ms
17,536 KB
testcase_28 AC 29 ms
12,928 KB
testcase_29 AC 39 ms
17,792 KB
testcase_30 AC 39 ms
17,664 KB
testcase_31 AC 36 ms
14,848 KB
testcase_32 AC 36 ms
14,848 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 10 ms
5,376 KB
testcase_37 AC 10 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// ここまで

fn convert(a: Vec<char>) -> Vec<(char, u8)> {
    let mut s = vec![];
    let mut i = 0;
    while i < a.len() {
        if i + 1 < a.len() && a[i + 1] == '?' {
            s.push((a[i], 1));
            i += 2;
        } else if i + 1 < a.len() && a[i + 1] == '*' {
            s.push((a[i], 2));
            i += 2;
        } else {
            s.push((a[i], 0));
            i += 1;
        }
    }
    s
}

use std::cmp::min;

fn run() {
    input! {
        a: chars,
        b: chars,
    }
    let a = convert(a);
    let b = convert(b);
    let mut dp = vec![vec![0; b.len() + 1]; a.len() + 1];
    for i in 1..=a.len() {
        if a[i - 1].1 > 0 {
            dp[i][0] = dp[i - 1][0];
        } else {
            dp[i][0] = dp[i - 1][0] + 1;
        }
    }
    for i in 1..=b.len() {
        if b[i - 1].1 > 0 {
            dp[0][i] = dp[0][i - 1];
        } else {
            dp[0][i] = dp[0][i - 1] + 1;
        }
    }
    for i in 1..=a.len() {
        for j in 1..=b.len() {
            let x = a[i - 1];
            let y = b[j - 1];
            dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1));
            if x.1 > 0 {
                dp[i][j] = min(dp[i][j], dp[i - 1][j]);
            }
            if y.1 > 0 {
                dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            }
            if x.0 == y.0 {
                dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
                if x.1 == 2 {
                    dp[i][j] = min(dp[i][j], dp[i][j - 1]);
                }
                if y.1 == 2 {
                    dp[i][j] = min(dp[i][j], dp[i - 1][j]);
                }
            }
        }
    }
    println!("{}", dp[a.len()][b.len()]);
}

fn main() {
    run();
}
0