結果

問題 No.1909 Detect from Substrings
ユーザー phspls
提出日時 2022-10-16 13:58:11
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,874 bytes
コンパイル時間 13,170 ms
コンパイル使用メモリ 401,396 KB
実行使用メモリ 36,236 KB
最終ジャッジ日時 2024-06-27 09:51:03
合計ジャッジ時間 17,809 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let m = nm[1];
    let words = (0..n).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp = temp.trim();
            temp.chars().collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();

    let s1 = &words[0];
    let s2 = &words[1];
    let front_same_cnt = (0..m).filter(|&i| s1[i] != s2[i]).nth(0).unwrap();
    let back_diff_point = (0..m).rev().filter(|&i| s1[i] != s2[i]).nth(0).unwrap();
    let t1 = format!("{}{}{}{}",
        (0..front_same_cnt).map(|i| s1[i].to_string()).collect::<Vec<_>>().join(""),
        (front_same_cnt..=back_diff_point).map(|i| s1[i].to_string()).collect::<Vec<_>>().join(""),
        s2[back_diff_point],
        (back_diff_point+1..m).map(|i| s1[i].to_string()).collect::<Vec<_>>().join("")
    ).chars().collect::<Vec<_>>();
    let t2 = format!("{}{}{}{}",
        (0..front_same_cnt).map(|i| s2[i].to_string()).collect::<Vec<_>>().join(""),
        (front_same_cnt..=back_diff_point).map(|i| s2[i].to_string()).collect::<Vec<_>>().join(""),
        s1[back_diff_point],
        (back_diff_point+1..m).map(|i| s2[i].to_string()).collect::<Vec<_>>().join("")
    ).chars().collect::<Vec<_>>();
    let is_ok = |s: &Vec<char>, t: &Vec<char>| {
        let front_same_cnt = (0..m+1).filter(|&i| i == m || s[i] != t[i]).nth(0).unwrap();
        (front_same_cnt..s.len()).filter(|&i| s[i] != t[i+1]).count() == 0
    };
    let mut result = 0;
    if (0..n).map(|i| is_ok(&words[i], &t1)).fold(true, |x, y| x && y) { result += 1; }
    if (0..n).map(|i| is_ok(&words[i], &t2)).fold(true, |x, y| x && y) { result += 1; }
    println!("{}", result);
}
0