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 OCaml (judge verdict: Time Limit Exceeded)

exception Not_equal of int

let cmp_sa rank a b n k =
  if rank.(a) <> rank.(b) then compare rank.(a) rank.(b)
  else
    compare
      (if a + k <= n then rank.(a+k) else (-1))
      (if b + k <= n then rank.(b+k) else (-1))

let construct_sa t n =
  let sa = Array.make (n + 1) 0 in
  let rank = Array.make (n + 1) 0 in
  let tmp = Array.make (n + 1) 0 in
  for i = 0 to n - 1 do
    sa.(i) <- i;
    rank.(i) <- Char.code t.[i];
  done;
  sa.(n) <- n;
  rank.(n) <- (-1);
  let k = ref 1 in
  while !k <= n do
    Array.sort (fun a b -> cmp_sa rank a b n !k) sa;
    tmp.(sa.(0)) <- 0;
    for i = 1 to n do
      tmp.(sa.(i)) <-
        tmp.(sa.(i-1)) + (if cmp_sa rank sa.(i-1) sa.(i) n !k = (-1) then 1 else 0);
    done;
    for i = 0 to n do rank.(i) <- tmp.(i) done;
    k := !k * 2;
  done;
  sa

let range_cmp t n offset p k =
  let m = n - offset in
  let m = if m < k then m else k in
  try
    for i = 0 to m - 1 do
      if t.[i+offset] <> p.[i] then
        raise (Not_equal (compare t.[i+offset] p.[i]));
    done;
    if m = k then 0
    else (-1)
  with Not_equal i -> i

let () =
  let t = read_line () in
  let n = String.length t in
  let sa = construct_sa t n in
  let q = read_int () in
  for _ = 0 to q - 1 do
    let p = read_line () in
    let k = String.length p in
    let l = ref 0 in
    let r = ref (n + 1) in
    while !l + 1 < !r do
      let m = (!l + !r) / 2 in
      let ret = range_cmp t n sa.(m) p k in
      if ret <= 0 then l := m
      else r := m;
    done;
    Printf.printf "%d\n" (if range_cmp t n sa.(!l) p k = 0 then 1 else 0);
  done