結果

問題 No.430 文字列検索
ユーザー tubo28tubo28
提出日時 2016-12-30 14:55:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,976 bytes
コンパイル時間 18,044 ms
コンパイル使用メモリ 378,596 KB
実行使用メモリ 15,604 KB
最終ジャッジ日時 2024-05-09 14:17:14
合計ジャッジ時間 14,846 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 41 ms
15,560 KB
testcase_02 AC 19 ms
5,376 KB
testcase_03 AC 19 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 40 ms
15,604 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 25 ms
5,808 KB
testcase_12 AC 25 ms
5,936 KB
testcase_13 AC 26 ms
5,808 KB
testcase_14 AC 23 ms
5,376 KB
testcase_15 AC 21 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 20 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::HashMap;

pub static MOD1:u64 = 1000000021;
pub static B1:u64 = 10000;
// pub static MOD2:u64 = 1000000033;
pub static B2:u64 = 10009;

fn main(){
    let mut sc = Scanner::new();
    while sc.ok() {
        let s:Vec<u64> = sc.next::<String>().chars()
            .map(|x| x as u64).collect();
        let mut mp = HashMap::new();
        let pow1:Vec<_> = (0..20).map(|x| modpow(B1, x)).collect();
        let pow2:Vec<_> = (0..20).map(|x| modpow(B2, x)).collect();

        for l in 1..11 {
            let mut h1 = 0;
            let mut h2 = 0;
            for i in 0..s.len() {
                h1 = (h1 * B1 + s[i]) % MOD1;
                h2 = (h2 * B2 + s[i]) % MOD1;
                if i >= l-1 {
                    *mp.entry(h1 << 32 | h2).or_insert(0) += 1;
                    h1 = (h1 + MOD1 - s[i+1-l] * pow1[l-1] % MOD1) % MOD1;
                    h2 = (h2 + MOD1 - s[i+1-l] * pow2[l-1] % MOD1) % MOD1;
                }
            }
        }

        let m = sc.next();
        let mut ans:u64 = 0;
        for _ in 0..m {
            let s:Vec<_> = sc.next::<String>().chars()
                .map(|x| x as u64).collect();
            let h1 = s.iter().fold(0, |acc, x| (acc * B1 + x) % MOD1);
            let h2 = s.iter().fold(0, |acc, x| (acc * B2 + x) % MOD1);
            let x = mp.get(&(h1 << 32 | h2)).or(Some(&0)).unwrap().clone();
            ans += x;
        }
        println!("{}", ans);
    }
}

fn modpow(x: u64, y: u64) -> u64 {
    if y == 0 {
        1
    } else if y%2 == 1 {
        x * modpow(x, y-1) % MOD1
    } else {
        let t = modpow(x, y/2);
        t * t % MOD1
    }
}

#[allow(dead_code)]
struct Scanner { token_buffer: Vec<String>, index: usize }

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner { token_buffer: vec![], index: 0 }
    }

    fn next<T>(&mut self) -> T where T: std::str::FromStr {
        self.wrapped::<T>().unwrap()
    }

    fn wrapped<T>(&mut self) -> Option<T> where T: std::str::FromStr {
        self.get_token().and_then(|s| s.parse::<T>().ok())
    }

    fn ok(&mut self) -> bool {
        !self.eof()
    }

    fn eof(&mut self) -> bool {
        !self.read_line() || self.index >= self.token_buffer.len()
    }

    fn get_token(&mut self) -> Option<&String> {
        if !self.read_line() {
            None
        } else {
            self.index += 1;
            Some(&self.token_buffer[self.index - 1])
        }
    }

    fn read_line(&mut self) -> bool {
        while self.index >= self.token_buffer.len() {
            let mut st = String::new();
            while st.trim() == "" {
                match std::io::stdin().read_line(&mut st) {
                    Ok(l) if l > 0 => continue,
                    _ => return false
                }
            }
            self.token_buffer = st.split_whitespace()
                .map(|x| x.to_string()).collect();
            self.index = 0;
        }
        true
    }
}
0