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)

let id x = x
             
let string_search s p =
  let sl = String.length s in
  let pl = String.length p in
  let sa = Array.init (sl+1) id in
  let rank = Array.init (sl+1) (fun i -> if i = sl then (-1) else Char.code s.[i]) in
  let cmp k i j =
    if rank.(i) = rank.(j) then
      let rik = if i + k <= sl then rank.(i+k) else (-1) in
      let rjk = if j + k <= sl then rank.(j+k) else (-1) in
      rik - rjk
    else rank.(i) - rank.(j) in
  let ra_rank k =
    let ta = Array.make (sl+1) 0 in
    let rec iter i =
      if i > sl then ()
      else begin
          ta.(sa.(i)) <- ta.(sa.(i-1)) + (if cmp k sa.(i) sa.(i-1) > 0 then 1 else 0);
          iter (i+1)
        end in
    let rec copy i =
      if i > sl then ()
      else begin
          rank.(i) <- ta.(i);
          copy (i+1)
        end in
    iter 1;
    copy 0 in
  let rec sort_sa k =
    if k > sl then ()
    else begin
        Array.sort (cmp k) sa;
        ra_rank k;
        sort_sa (2 * k)
      end in
  let rec search l r =
    if r - l <= 1 then
      try String.sub s sa.(r) pl = p with _ -> false
    else
      let m = (l + r) / 2 in
      match compare (String.sub s sa.(m) (min pl (sl - sa.(m)))) p with
        0 -> true
      | x when x < 0 -> search m r
      | _ -> search l m
  in
  sort_sa 1;
  search 0 (sl+1)
      
let () =
  let t = read_line () in
  let q = read_int () in
  let rec loop x =
    if x = 0 then ()
    else let p = read_line () in
         (if string_search t p then "1" else "0") |> print_endline;
         loop (x-1)
  in loop q