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(pub T); impl PartialOrd for Rev { fn partial_cmp(&self, other: &Rev) -> Option { other.0.partial_cmp(&self.0) } } impl Ord for Rev { fn cmp(&self, other: &Rev) -> std::cmp::Ordering { other.0.cmp(&self.0) } } fn read() -> 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::>() }; ([$t:tt; $len:expr]) => { (0..$len).map(|_| read!($t)).collect::>() }; (chars) => { read!(String).chars().collect::>() }; (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; fn cumulation_query(&self, a: &usize, b: &usize) -> i64; } impl VecExt for Vec { fn cumulation(&self) -> Vec { 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 { fn lower_bound(&self, x: &T) -> usize; fn upper_bound(&self, x: &T) -> usize; } impl SliceExt 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 = BTreeMap::new(); let tmp: Vec = 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 = 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); }