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
{
    struct Pattern
    {
        internal string str;
        internal int id;
        internal bool find;
    }

    class SearchString
    {
        readonly int MOD = 1000000007;
        readonly int C = 407;

        public void SearchStrings(string text, Pattern[] patterns)
        {
            Array.Sort(patterns, (a, b) => a.str.Length - b.str.Length);

            int curLen = 0;
            long[] txtTable = new long[text.Length];
            long[] patTable;

            for (int i = 0; i < patterns.Length; i++)
            {
                string pat = patterns[i].str;

                if (pat.Length > text.Length) continue;

                if (curLen != pat.Length)
                {
                    txtTable = GenHash(text, text.Length, pat.Length);
                    curLen = pat.Length;
                }

                patTable = GenHash(pat, pat.Length, pat.Length);

                for (int j = 0; j <= text.Length - pat.Length; j++)
                {
                    if (txtTable[j] == patTable[0])
                    {
                        if (text.Substring(j, pat.Length) == pat)
                        {
                            patterns[i].find = true;
                            break;
                        }
                    }
                }
            }

            Array.Sort(patterns, (a, b) => a.id - b.id);

            StringBuilder sb = new StringBuilder();

            foreach (var p in patterns)
            {
                sb.AppendLine(p.find ? "1" : "0");
            }
            Console.Write(sb);
        }

        private long[] GenHash(string v, int n, int m)
        {
            long[] ret = new long[n];

            long cn = 1;

            for (int i = 0; i < m; i++) cn = (cn * C) % MOD;

            long hs = 0;

            for (int i = 0; i < m; i++) hs = (hs * C + v[i]) % MOD;

            for (int i = 0; i <= n - m; i++)
            {
                ret[i] = hs;

                if (i + m < n) hs = (hs * C - v[i] * cn + v[i + m]) % MOD;

                hs = (hs < 0) ? hs + MOD : hs;
            }
            return ret;
        }
    }

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

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

            Pattern[] patterns = new Pattern[Q];
            Pattern temp;

            for (int i = 0; i < Q; i++)
            {
                temp.str = Console.ReadLine();
                temp.id = i;
                temp.find = false;

                patterns[i] = temp;
            }

            SearchString SS = new SearchString();
            SS.SearchStrings(text, patterns);
        }
    }
}