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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class KMP
    {
        int[] lps;

        public int IndexOf(string text, string pattern)
        {
            int M = pattern.Length;
            int N = text.Length;
            int i = 0;
            int j = 0;

            ComputeLPS(pattern, M);

            while (i < N)
            {
                if (pattern[j] == text[i])
                {
                    j++;
                    i++;
                }

                if (j == M)
                {
                    return (i - j);
                }

                else if (i < N && pattern[j] != text[i])
                {
                    if (j != 0) j = lps[j - 1];
                    else i++;
                }
            }
            return -1;
        }

        private void ComputeLPS(string pattern, int M)
        {
            lps = new int[M];
            lps[0] = 0;

            int len = 0;
            int i = 1;

            while (i < M)
            {
                if (pattern[i] == pattern[len])
                {
                    len++;
                    lps[i] = len;
                    i++;
                }
                else
                {
                    if (len != 0)
                    {
                        len = lps[len - 1];
                    }
                    else
                    {
                        lps[i] = 0;
                        i++;
                    }
                }
            }
        }
    }

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

            int n = int.Parse(Console.ReadLine());

            StringBuilder sb = new StringBuilder();
            KMP kmp = new KMP();

            for (int i = 0; i < n; i++)
            {
                string pattern = Console.ReadLine();

                int index = kmp.IndexOf(text, pattern);

                sb.AppendLine(index >= 0 ? "1" : "0");
            }
            Console.Write(sb);
        }
    }
}