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 C# (judge verdict: Runtime Error)

using System;
using System.Linq;
using System.Text;

namespace ALDS1_14_D
{
    class SuffixArray
    {
        internal int[] sa, rank, tmp;
        internal string S;
        internal int n, k;

        public SuffixArray(string SS)
        {
            S = SS;
            n = S.Length;
            sa = new int[n + 1];
            rank = new int[n + 1];
            tmp = new int[n + 1];
            Construct_SA();
        }

        public int Compare_SA(int i, int j)
        {
            if (rank[i] > rank[j]) return 1;
            if (rank[i] < rank[j]) return -1;

            int ri = i + k <= n ? rank[i + k] : -1;
            int rj = j + k <= n ? rank[j + k] : -1;
            if (ri > rj) return 1;
            if (ri < rj) return -1;
            return 0;
        }

        public void Construct_SA()
        {
            for (int i = 0; i <= n; i++)
            {
                sa[i] = i;
                rank[i] = i < n ? S[i] : -1;
            }

            for (k = 1; k <= n; k *= 2)
            {
                Array.Sort(sa, Compare_SA);

                /*Console.WriteLine();
                Console.WriteLine(0 + " " + sa[0] + "   " + rank[sa[0]]);
                for (int i = 1; i <= n; i++)
                {
                    Console.WriteLine(i + " " + sa[i] + " " + S[sa[i]] + " " + rank[sa[i]]);
                }*/


                tmp[sa[0]] = 0;
                for (int i = 1; i <= n; i++)
                {
                    tmp[sa[i]] = tmp[sa[i - 1]] + ((Compare_SA(sa[i - 1], sa[i]) < 0) ? 1 : 0);
                }
                for (int i = 0; i <= n; i++)
                {
                    rank[i] = tmp[i];
                }

            }
        }

        public bool Contains(string pattern)
        {
            int l = 1, r = n + 1;
            int pl = pattern.Length;

            while (l < r)
            {
                int m = (l + r) / 2;

                string subStr = (sa[m] + pl > r) ? S.Substring(sa[m]) : S.Substring(sa[m], pl);


                int comp = subStr.CompareTo(pattern);

                //Console.WriteLine(l + " " + r + " " + m + " " + subStr + " " + comp);
                if (comp == 0) return true;

                if (comp < 0) l = m + 1;
                else r = m;
            }
            return false;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var T = Console.ReadLine();

            SuffixArray SA = new SuffixArray(T);
            StringBuilder sb = new StringBuilder();

            var Q = int.Parse(Console.ReadLine());

            for (int i = 0; i < Q; i++)
            {
                var P = Console.ReadLine();

                sb.AppendLine(SA.Contains(P) ? "1" : "0");
            }
            Console.Write(sb);
        }
    }
}