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 = 1_000_000_007; struct RollingHash2 { rh1: RollingHash, rh2: RollingHash, } impl RollingHash2 { fn new(s: &Vec) -> Self { let (rh1, rh2) = RollingHash::create_recommended_pair(&s); RollingHash2 { rh1: rh1, rh2: rh2 } } fn get(&self, l: usize, r: usize) -> (i64, i64) { (self.rh1.get(l, r), self.rh2.get(l, r)) } } struct RollingHash { modulo: i64, power: Vec, hash: Vec, } impl RollingHash { fn create_recommended_pair(s: &Vec) -> (Self, Self) { ( RollingHash::new(&s, 1009, 1_000_000_007), RollingHash::new(&s, 1007, 1_000_000_009), ) } fn new(s: &Vec, base: i64, modulo: i64) -> Self { let n = s.len(); let mut power = vec![1; n + 1]; let mut hash = vec![0; n + 1]; for i in 0..n { power[i + 1] = power[i] * base % modulo; hash[i + 1] = (hash[i] * base + s[i]) % modulo; } RollingHash { modulo: modulo, power: power, hash: hash, } } fn get(&self, l: usize, r: usize) -> i64 { (self.hash[r] + self.modulo - (self.hash[l] * self.power[r - l]) % self.modulo) % self.modulo } } fn main() { input!(s: String, m: usize); use std::collections::BTreeMap; let svec: Vec = s.chars().map(|c| c as i64).collect(); let mut map: BTreeMap<(i64, i64,), i64> = BTreeMap::new(); let rh = RollingHash2::new(&svec); let mut ans = 0i64; 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 cvec: Vec = c.chars().map(|c| c as i64).collect(); let crh = RollingHash2::new(&cvec); let h = crh.get(0, c.len()); if map.contains_key(&h) { ans += map[&h]; } } println!("{}", ans); }