Problem:
String Search

Determine whether a text T includes a pattern P. Your program should answer for given queries consisting of P_i.

Input

In the first line, a text T is given. In the second line, an integer Q denoting the number of queries is given. In the following Q lines, the patterns P_i are given respectively.

Output

For each question, print 1 if the text includes P_i, or print 0 otherwise.

Constraints

1 ≤ length of T ≤ 1000000

1 ≤ length of P_i ≤ 1000

1 ≤ Q ≤ 10000

The input consists of alphabetical characters and digits

Sample Input 1

aabaaa
4
aa
ba
bb
xyz

Sample Output 1

1
1
0
0

Solution in Rust (judge verdict: Accepted)

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

//http://wk1080id.hatenablog.com/entry/2018/12/25/005926
fn gen_suffix_array(a: &Vec<usize>) -> Vec<usize> {
    let mut s = a.clone();
    for s in &mut s {
        *s += 1;
    }
    s.push(0);
    let n = s.len();
    let m = s.iter().max().unwrap() + 1;
    // k = 0
    let mut cnt = vec![0; m];
    let mut p = vec![0; n];
    let mut c = vec![0; n];
    for s in &s {
        cnt[*s] += 1;
    }
    for i in 1..m {
        cnt[i] += cnt[i - 1];
    }
    for i in 0..n {
        let c = s[i];
        cnt[c] -= 1;
        p[cnt[c]] = i;
    }
    c[p[0]] = 0;
    let mut kind = 1;
    for i in 1..n {
        if s[p[i]] != s[p[i - 1]] {
            kind += 1;
        }
        c[p[i]] = kind - 1;
    }
    let mut k = 1;
    while k < n {
        let mut next_p = vec![0; n];
        for i in 0..n {
            next_p[i] = (p[i] + n - k) % n;
        }
        let mut cnt = vec![0; kind];
        for &p in &next_p {
            cnt[c[p]] += 1;
        }
        for i in 1..kind {
            cnt[i] += cnt[i - 1];
        }
        for &pn in next_p.iter().rev() {
            let k = c[pn];
            cnt[k] -= 1;
            p[cnt[k]] = pn;
        }
        let mut next_c = vec![0; n];
        next_c[p[0]] = 0;
        kind = 1;
        for i in 1..n {
            let prev = (c[p[i - 1]], c[(p[i - 1] + k) % n]);
            let cur  = (c[p[i]], c[(p[i] + k) % n]);
            if prev != cur {
                kind += 1;
            }
            next_c[p[i]] = kind - 1;
        }
        c = next_c;
        k <<= 1;
    }
    p
}

use std::io::Write;

fn convert(c: char) -> usize {
    if '0' <= c && c <= '9' {
        c.to_digit(10).unwrap() as usize
    } else if 'a' <= c && c <= 'z' {
        c as usize - 'a' as usize + 10
    } else {
        c as usize - 'A' as usize + 36
    }
}

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    input! {
        s: chars,
        q: usize,
        a: [chars; q],
    }
    let s_map: Vec<usize> = s.into_iter().map(convert).collect();
    let su = gen_suffix_array(&s_map);
    let s = s_map;
    for a in a {
        let a: Vec<usize> = a.into_iter().map(convert).collect();
        let mut l = 0;
        let mut r = su.len() - 1;
        while r - l > 1 {
            let m = (l + r) / 2;
            if *a.as_slice() <= s[su[m]..] {
                r = m;
            } else {
                l = m;
            }
        }
        let ans = if s.len() - su[r] >= a.len() && *a.as_slice() == s[su[r]..(su[r] + a.len())] {
            1
        } else {
            0
        };
        writeln!(out, "{}", ans).unwrap();
    }
}

fn main() {
    run();
}