結果

問題 No.1909 Detect from Substrings
ユーザー phspls
提出日時 2022-10-23 12:35:30
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,789 bytes
コンパイル時間 17,576 ms
コンパイル使用メモリ 383,092 KB
実行使用メモリ 36,124 KB
最終ジャッジ日時 2024-07-02 05:51:02
合計ジャッジ時間 18,574 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

fn is_ok(s: &Vec<char>, t: &Vec<char>) -> bool {
    let m = s.len();
    let diff_point = (0..=m).filter(|&i| if i == m { true } else { s[i] != t[i] }).nth(0).unwrap();
    !(diff_point..m).any(|i| s[i] != t[i+1])
}

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 same_s = (0..m).filter(|&i| s1[i] != s2[i]).nth(0).unwrap();
    let same_e = (0..m).rev().filter(|&i| s1[i] != s2[i]).nth(0).unwrap()+1;
    let t1 = format!("{}{}{}{}",
        (0..same_s).map(|i| s1[i].to_string()).collect::<Vec<_>>().join(""),
        (same_s..same_e).map(|i| s1[i].to_string()).collect::<Vec<_>>().join(""),
        s2[same_e-1],
        (same_e..m).map(|i| s1[i].to_string()).collect::<Vec<_>>().join("")
    )
    .chars().collect::<Vec<_>>();
    let t2 = format!("{}{}{}{}",
        (0..same_s).map(|i| s1[i].to_string()).collect::<Vec<_>>().join(""),
        (same_s..same_e).map(|i| s2[i].to_string()).collect::<Vec<_>>().join(""),
        s1[same_e-1],
        (same_e..m).map(|i| s1[i].to_string()).collect::<Vec<_>>().join("")
    )
    .chars().collect::<Vec<_>>();

    let mut result = 0usize;
    if (0..n).filter(|&i| is_ok(&words[i], &t1)).count() == n {
        result += 1;
    }
    if (0..n).filter(|&i| is_ok(&words[i], &t2)).count() == n {
        result += 1;
    }
    println!("{}", result);
}
0