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: Wrong Answer)

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

namespace ALDS1_14_D
{
    class Program
    {
        static StringBuilder sb = new StringBuilder();
        static string S, T;
        static int[] sa;
        static int n, k;
        static int[] rank, tmp;
        static int Q;

        static void Main(string[] args)
        {
            S = Console.ReadLine();
            n = S.Length;
            sa = new int[n + 1];
            rank = new int[n + 1];
            tmp = new int[n + 1];
            construct_sa();



            Q = int.Parse(Console.ReadLine());
            for (int i = 0; i < Q; i++)
            {
                T = Console.ReadLine();
                sb.AppendLine(contain() ? "1" : "0");
            }
            Console.Write(sb);
        }

        static bool contain()
        {
            int a = 0, b = S.Length + 1;
            string d = "";
            while (a != b)
            {              
                int c = (a + b) / 2;
                //S???sa[c]???????????????|T|????????¨T?????????


                d = "";
                for (int i = 0; i < T.Length; i++)
                {
                    if (sa[c] + i >= b - 1) break;
                    d += S[sa[c] + i];
                }
                
                int dc = d.CompareTo(T);
                //Console.WriteLine(a + " " + b + " " + c + " " + d +" "+ dc);
                if (dc == 0) return true;
                if (dc < 0) a = c + 1;
                else b = c; 
            }
            return false;

        }

        //(rank[i], rank[i + k])??¨(rank[j], rank[j + k])?????????
        static bool 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;
        }
        
        //?????????S?????\?°??????????????§????
        static void construct_sa()
        {
            //?????????1???????????????????????????????????????????????°??????
            for (int i = 0; i <= n; i++)
            {
                sa[i] = i;
                rank[i] = i < n ? S[i] : -1;
            }

            //k????????????????????????????????????????????¨???????????????2k????????§???????????????
            for (k = 1; k <= n; k *= 2)
            {
                //Array.Sort(rank, sa);
                sa = sa.OrderBy(s => rank[s]).ToArray();


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



                //?????£??????tmp????¬?????????????????¨??????????????????????rank????§????
                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]) ? 1 : 0);
                }
                for (int i = 0; i <= n; i++)
                {
                    rank[i] = tmp[i];
                }
            }
        }
    }
}