結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-05-05 09:03:46 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,735 bytes |
| コンパイル時間 | 10,832 ms |
| コンパイル使用メモリ | 400,268 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-11-10 00:24:07 |
| 合計ジャッジ時間 | 14,448 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 12 |
コンパイルメッセージ
warning: unnecessary trailing semicolon
--> src/main.rs:2:103
|
2 | macro_rules ! for_ { ( $ init : stmt ; $ cond : expr ; $ incr : expr , $ body : block ) => ( { $ init ; while $ cond { $ body $ incr ; } ...
| ^ help: remove this semicolon
...
43 | / for_!(let mut i = 0; i + key_len <= base_len; i += 1, {
44 | | if key_h == base_h {ret.push(i);}
45 | | if i + key_len < base_len {
46 | | let mut p = base_h * B;
... |
55 | | }
56 | | });
| |______- in this macro invocation
|
= note: `#[warn(redundant_semicolons)]` on by default
= note: this warning originates in the macro `for_` (in Nightly builds, run with -Z macro-backtrace for more info)
ソースコード
#[allow(unused_macros)]
macro_rules ! for_ { ( $ init : stmt ; $ cond : expr ; $ incr : expr , $ body : block ) => ( { $ init ; while $ cond { $ body $ incr ; } } ) ; }
fn main() {
use std::io::Read;
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut iter = buf.split_whitespace();
let s: String = iter.next().unwrap().parse::<String>().unwrap();
let m: usize = iter.next().unwrap().parse::<usize>().unwrap();
let mut cnt = 0;
for _ in 0..m {
let c: String = iter.next().unwrap().parse::<String>().unwrap();
cnt += search_rh(s.as_bytes(), c.as_bytes()).len();
}
println!("{}", cnt);
}
#[doc = " O(len(base) + len(key))"]
#[allow(dead_code)]
pub fn search_rh(base: &[u8], key: &[u8]) -> Vec<usize> {
const B: i64 = 9973;
const M: i64 = 1_000_000_007;
let key_len = key.len();
let base_len = base.len();
if key_len > base_len {return vec![];}
let mut t = 1i64;
for _ in 0..key_len {
t *= B;
t %= M;
}
let mut key_h = 0;
let mut base_h = 0;
for i in 0..key_len {
key_h = ((key_h * B) % M + (key[i] as i64) % M) % M;
base_h = ((base_h * B) % M + (base[i] as i64) % M) % M;
}
let mut ret = vec![];
for_!(let mut i = 0; i + key_len <= base_len; i += 1, {
if key_h == base_h {ret.push(i);}
if i + key_len < base_len {
let mut p = base_h * B;
let mut q = base[i + key_len] as i64;
let mut r = base[i] as i64 * t;
p %= M;
q %= M;
r %= M;
let mut z = ((p + q) % M - r) % M;
if z < 0 {z += M;}
base_h = z;
}
});
ret
}