結果

問題 No.430 文字列検索
ユーザー pianonekopianoneko
提出日時 2019-09-24 03:27:39
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 5,362 bytes
コンパイル時間 5,755 ms
コンパイル使用メモリ 179,480 KB
実行使用メモリ 14,264 KB
最終ジャッジ日時 2023-10-19 08:38:07
合計ジャッジ時間 3,960 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 160 ms
14,264 KB
testcase_02 AC 32 ms
4,348 KB
testcase_03 AC 34 ms
4,348 KB
testcase_04 AC 1 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 162 ms
14,264 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 11 ms
4,348 KB
testcase_11 AC 82 ms
5,552 KB
testcase_12 AC 81 ms
5,552 KB
testcase_13 WA -
testcase_14 AC 72 ms
4,760 KB
testcase_15 AC 64 ms
4,496 KB
testcase_16 AC 40 ms
4,348 KB
testcase_17 AC 37 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::BufRead`
 --> Main.rs:2:5
  |
2 | use std::io::BufRead;
  |     ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused imports: `Add`, `Div`, `Mul`, `Sub`
 --> Main.rs:4:16
  |
4 | use std::ops::{Add, Div, Mul, Sub};
  |                ^^^  ^^^  ^^^  ^^^

warning: variable does not need to be mutable
   --> Main.rs:203:13
    |
203 |         let mut cvec: Vec<i64> = c.chars().map(|c| c as i64).collect();
    |             ----^^^^
    |             |
    |             help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

warning: function `read` is never used
  --> Main.rs:22:4
   |
22 | fn read<T: FromStr>() -> T {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: static `MOD` is never used
   --> Main.rs:144:8
    |
144 | static MOD: i64 = 1e9 as i64 + 7;
    |        ^^^

warning: variable `S` should have a snake case name
   --> Main.rs:186:12
    |
186 |     input!(S: String, M: usize);
    |            ^ help: convert the identifier to snake case (notice the capitalization): `s`
    |
    = note: `#[warn(non_snake_case)]` on by default

warning: variable `M` should have a snake case name
   --> Main.rs:186:23
    |
186 |     input!(S: String, M: usize);
    |                       ^ help: convert the identifier to snake case: `m`

warning: 7 warnings emitted

ソースコード

diff #

use std::fmt::Debug;
use std::io::BufRead;
use std::io::{stdin, Read};
use std::ops::{Add, Div, Mul, Sub};
use std::str::FromStr;

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<std::cmp::Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> std::cmp::Ordering {
        other.0.cmp(&self.0)
    }
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();

    token.parse().ok().expect("failed to parse token")
}

macro_rules! read {
    (($($t:tt),*)) => {
        ( $(read!($t)),* )
    };
    ([[$t:tt; $len1:expr]; $len2:expr]) => {
        (0..$len2).map(|_| read!([$t; $len1])).collect::<Vec<_>>()
    };

    ([$t:tt; $len:expr]) => {
        (0..$len).map(|_| read!($t)).collect::<Vec<_>>()
    };

    (chars) => {
        read!(String).chars().collect::<Vec<char>>()
    };

    (usize1) => {
        read!(usize) - 1
    };

    ($t:ty) => {{
        let stdin = stdin();
        let stdin = stdin.lock();
        let token: String = stdin
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect();

        token.parse::<$t>().unwrap()
    }};
}

macro_rules! input {
    (mut $name:ident: $t:tt, $($r:tt)*) => {
        let mut $name = read!($t);
        input!($($r)*);
    };

    (mut $name:ident: $t:tt) => {
        let mut $name = read!($t);
    };

    ($name:ident: $t:tt, $($r:tt)*) => {
        let $name = read!($t);
        input!($($r)*);
    };

    ($name:ident: $t:tt) => {
        let $name = read!($t);
    };
}

trait VecExt {
    fn cumulation(&self) -> Vec<i64>;
    fn cumulation_query(&self, a: &usize, b: &usize) -> i64;
}

impl VecExt for Vec<i64> {
    fn cumulation(&self) -> Vec<i64> {
        let mut vec = vec![0; self.len() + 1];
        for i in 0..self.len() {
            vec[i + 1] = self[i] + vec[i];
        }
        return vec;
    }

    fn cumulation_query(&self, left: &usize, right: &usize) -> i64 {
        return self[*right] - self[*left];
    }
}

trait SliceExt<T> {
    fn lower_bound(&self, x: &T) -> usize;
    fn upper_bound(&self, x: &T) -> usize;
}

impl<T: Ord> SliceExt<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut ng = -1;
        let mut ok = self.len() as i64;

        while (ok - ng).abs() > 1 {
            let mid = (ok + ng) / 2;
            if self[mid as usize] >= *x {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        return ok as usize;
    }

    fn upper_bound(&self, x: &T) -> usize {
        let mut ng = -1;
        let mut ok = self.len() as i64;
        while (ok - ng).abs() > 1 {
            let mid = (ok + ng) / 2;
            if self[mid as usize] > *x {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        return ok as usize;
    }
}

static MOD: i64 = 1e9 as i64 + 7;

pub struct RollingHash {
    hash_pow_list: Vec<(i64, Vec<(i64, i64)>)>,
}
impl RollingHash {
    pub fn new(s: &[i64]) -> RollingHash {
        RollingHash::with_base_mod_pairs(s, &[(1009, 1000000033), (9973, 1000000087)])
    }
    pub fn with_base_mod_pairs(s: &[i64], base_mod_pairs: &[(i64, i64)]) -> RollingHash {
        let hp_list = base_mod_pairs
            .iter()
            .map(|&(base, m)| {
                let mut hp = Vec::with_capacity(s.len() + 1);
                hp.push((0, 1));
                for (i, &x) in s.iter().enumerate() {
                    let (h, p) = hp[i];
                    hp.push(((h + x) * base % m, p * base % m));
                }
                (m, hp)
            })
            .collect();
        RollingHash {
            hash_pow_list: hp_list,
        }
    }
    pub fn get(&self, l: usize, r: usize) -> i64 {
        self.hash_pow_list
            .iter()
            .map(|&(m, ref hp)| (hp[r].0 + m - hp[l].0 * hp[r - l].1 % m) % m)
            .fold(0, |a, b| a ^ b)
    }
    pub fn len(&self) -> usize {
        self.hash_pow_list
            .first()
            .map(|v| v.1.len() - 1)
            .unwrap_or(0)
    }
}


fn main() {
    input!(S: String, M: usize);
    use std::collections::BTreeMap;
    let mut map: BTreeMap<i64, usize> = BTreeMap::new();
    let tmp: Vec<i64> = S.chars().map(|c| c as i64).collect();
    let rh = RollingHash::new(&tmp);
    let mut ans = 0;
    for i in 0..S.len() {
        for j in i..std::cmp::min(i + 10, S.len()) {
            let hash = rh.get(i, j+1
        );
            let count = map.entry(hash).or_insert(0);
            *count += 1;
        }
    }

    for _ in 0..M {
        input!(c: String);
        let mut cvec: Vec<i64> = c.chars().map(|c| c as i64).collect();
        let crh = RollingHash::new(&cvec);
        match map.get(&crh.get(0, c.len())) {
            Some(&x) => {
                ans += x;
            }
            _ => (),
        };
    }

    println!("{}", ans);
}
0