結果

問題 No.430 文字列検索
ユーザー akakimidoriakakimidori
提出日時 2019-08-13 06:08:20
言語 Rust
(1.77.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 4,445 ms
コンパイル使用メモリ 170,668 KB
実行使用メモリ 15,972 KB
最終ジャッジ日時 2023-10-19 10:47:29
合計ジャッジ時間 5,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 39 ms
15,972 KB
testcase_02 AC 14 ms
4,348 KB
testcase_03 AC 14 ms
4,348 KB
testcase_04 AC 0 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 38 ms
15,972 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 22 ms
6,288 KB
testcase_12 AC 22 ms
6,288 KB
testcase_13 AC 22 ms
6,288 KB
testcase_14 AC 18 ms
4,396 KB
testcase_15 AC 16 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 14 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::BufRead;

fn run() {
    let stdin = std::io::stdin();
    let mut stdin = stdin.lock();
    let mut s = String::new();
    stdin.read_line(&mut s).unwrap();
    let s: Vec<char> = s.trim().chars().collect();
    let mut dp = vec![0 as u64; s.len() + 1];
    let mut pow = vec![1; s.len() + 1];
    let r = 27;
    let mut map = std::collections::HashMap::new();
    for i in 0..s.len() {
        dp[i + 1] = dp[i] * r + (s[i] as u64 + 1 - 'A' as u64);
        pow[i + 1] = pow[i] * r;
        for j in 0..std::cmp::min(10, i + 1) {
            let v = dp[i + 1] - dp[i - j] * pow[j + 1];
            let po = map.entry(v).or_insert(0);
            *po += 1;
        }
    }
    let mut ans = 0;
    let mut s = String::new();
    stdin.read_line(&mut s).unwrap();
    let m: usize = s.split_whitespace().next().unwrap().parse().ok().unwrap();
    for _ in 0..m {
        s.clear();
        stdin.read_line(&mut s).unwrap();
        let s: Vec<char> = s.trim().chars().collect();
        let mut hash = 0;
        for c in s.iter() {
            hash = r * hash + (*c as u64 + 1 - 'A' as u64);
        }
        match map.get(&hash) {
            Some(&c) => ans += c,
            None => (),
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}
0