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: Accepted)

using System;
using System.IO;
 
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 rank[i] - rank[j];
             
            int ri = i + k  <= n ? rank[i + k] : -1;
            int rj = j + k <= n ? rank[j + k] : -1;
 
            return  ri - rj; 
        }
         
        public void Construct_SA()
        {
            for(int i = 0; i < n; i++)
            {
                sa[i] = i;
                rank[i] = S[i];
            }
            sa[n] = n; rank[n] = -1;
             
            for(k = 1; k <= n; k <<= 1)
            {
                Array.Sort(sa, Compare_SA);
                 
                int d;
                d = tmp[sa[0]] = 0;
                
                for (int i = 1; i <= n; i++)
                {
                    d = tmp[sa[i]] = d + ((Compare_SA(sa[i - 1], sa[i]) < 0) ? 1 : 0);
                }
                
                for (int i = 0; i <= n; i++)
                {
                    rank[i] = tmp[i];
                }
  
            }
        }
         
        public string Contains(string pattern)
        {
            int l = 1, r = n + 1;
            int pl = pattern.Length;
             
            while(l < r)
            {
                int m = (l + r) >> 1;
                 
                string subStr = (sa[m] + pl > n) ? S.Substring(sa[m]) : S.Substring(sa[m], pl);
                     
                int comp = subStr.CompareTo(pattern);
                if (comp == 0) return "1";
                 
                if (comp < 0) l = m + 1;
                else r = m;
            }
            return "0";
        }
    }
     
     
    class Program
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
          
            var T = Console.ReadLine();
            var SA = new SuffixArray(T);
            var Q = int.Parse(Console.ReadLine());
  
            for (int i = 0; i < Q; i++)
            {
                var P = Console.ReadLine();
  
                Console.WriteLine(SA.Contains(P));
            }
            Console.Out.Flush();
        }
    }
}