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.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
        var s=sc.S;
        var sa=new Sa1(s,'0','z');
        int n=sc.I;
        StringBuilder sb=new StringBuilder();
        for(int i = 0;i<n;i++) {sb.Append((sa.Fa1(sc.S)!=-1?1:0)+"\n");}
        Console.Write(sb);
	}
}
public class Sa1{
	private readonly int n,m,o;
	public readonly string s;
	public readonly int[] sa;
	public Sa1(string s,int a,int z){
		m=z-a+2;
		o=a-1;
		s+=(char)o;
		this.s=s;
		n=s.Length;
		int[] h=new int[n];
		var lms=new List<int>();
		int[] sub=new int[n];
		int[] hi=new int[m];
		int[] hc1=new int[m];
		int[] hc2=new int[m];
		int[] hc3=new int[m];
		hi[s[n-1]-o]=hc2[s[n-1]-o]=hc3[s[n-1]-o]=1;
		for(int i = n-2,k=n-1;i>=0;i--) {
			h[i]=s[i]!=s[i+1]?(s[i]>s[i+1]?1:0):h[i+1];
			hi[s[i]-o]++;
			if(h[i]==1&&h[i+1]==0){
				h[i+1]=2;
				lms.Add(i+1);
				sub[i+1]=k-i;
				k=i+1;
			}
		}
		for(int i = 1;i<m;i++) {
			hi[i]+=hi[i-1];
			hc2[i]=hc3[i]=hi[i];
			hc1[i]=hi[i-1];
		}
		int c=lms.Count;
		sa=new int[n];
		for(int i = 0;i<c;i++) {
			hc2[s[lms[i]]-o]--;
			sa[hc2[s[lms[i]]-o]]=lms[i];
		}
		for(int i = 0;i<n;i++) {
			if(sa[i]>0&&h[sa[i]-1]==1){
				sa[hc1[s[sa[i]-1]-o]]=sa[i]-1;
				hc1[s[sa[i]-1]-o]++;
			}
		}
		for(int i = n-1;i>=0;i--) {
			if(sa[i]>0&&h[sa[i]-1]!=1){
				hc3[s[sa[i]-1]-o]--;
				sa[hc3[s[sa[i]-1]-o]]=sa[i]-1;
			}
		}
		int[] lmsn=new int[c];
		for(int i = 0,j=c-1;i<n&&j>=0;i++) {
			if(h[sa[i]]==2){lmsn[j]=sa[i];j--;}
		}
		int[] lmsz=new int[n];
		int[] lmsc=new int[c];
		bool jf=false;
		int ki=0;
		for(int i = c-2;i>=0;i--) {
			if(sub[lmsn[i]]==sub[lmsn[i+1]]){
				bool bo=false;
				for(int j = 0;j<sub[lmsn[i]];j++) {if(s[lmsn[i]+j]!=s[lmsn[i+1]+j]){bo=true;break;}}
				if(bo){ki++;}
				else{jf=true;}
			}
			else{ki++;}
			lmsz[lmsn[i]]=ki;
		}
		for(int i = c-1;i>=0;i--) {lmsc[c-i-1]=lmsz[lms[i]];}
		hc2[0]=hc3[0]=hi[0];
		hc1[0]=0;
		for(int i = 1;i<m;i++) {
			hc2[i]=hc3[i]=hi[i];
			hc1[i]=hi[i-1];
		}
		if(jf){
			lmsn=Fu2(lmsc,ki+1);
			for(int i = 0;i<c;i++) {lmsn[i]=lms[c-1-lmsn[i]];}
		}
		sa=new int[n];
		for(int i = 0;i<c;i++) {
			hc2[s[lmsn[i]]-o]--;
			sa[hc2[s[lmsn[i]]-o]]=lmsn[i];
		}
		for(int i = 0;i<n;i++) {
			if(sa[i]>0&&h[sa[i]-1]==1){
				sa[hc1[s[sa[i]-1]-o]]=sa[i]-1;
				hc1[s[sa[i]-1]-o]++;
			}
		}
		for(int i = n-1;i>=0;i--) {
			if(sa[i]>0&&h[sa[i]-1]!=1){
				hc3[s[sa[i]-1]-o]--;
				sa[hc3[s[sa[i]-1]-o]]=sa[i]-1;
			}
		}
	}
	public int Fa1(string t){return Bs(t);}
	private int Bs(string t){
		int l=t.Length;
		int lb=-1,mid=0,ub=n-1;
		while(ub-lb>1){
			mid=(ub+lb)/2;
			int tm=Min(l,n-sa[mid])-1,ti=0;
			while(s[sa[mid]+ti]==t[ti]&&ti<tm){ti++;}
			if(s[sa[mid]+ti]>=t[ti]){ub=mid;}
			else{lb=mid;}
		}
		bool bo=true;
		for(int i = 0;i<l;i++) {
			if(s[sa[ub]+i]!=t[i]){bo=false;break;}
		}
		return bo?ub:-1;
	}
	private int[] Fu2(IReadOnlyList<int> s,int m){
		int n=s.Count;
		var lms=new List<int>();
		int[] h=new int[n],sub=new int[n],sa=new int[n];
		int[] hi=new int[m],hc1=new int[m],hc2=new int[m],hc3=new int[m];
		hi[s[n-1]]=hc2[s[n-1]]=hc3[s[n-1]]=1;
		for(int i = n-2,k=n-1;i>=0;i--) {
			h[i]=s[i]!=s[i+1]?(s[i]>s[i+1]?1:0):h[i+1];
			hi[s[i]]++;
			if(h[i]==1&&h[i+1]==0){
				h[i+1]=2;
				lms.Add(i+1);
				sub[i+1]=k-i;
				k=i+1;
			}
		}
		int lm=lms.Count;
		for(int i = 1;i<m;i++) {
			hi[i]+=hi[i-1];
			hc2[i]=hc3[i]=hi[i];
			hc1[i]=hi[i-1];
		}
		for(int i = 0;i<lm;i++) {
			hc2[s[lms[i]]]--;
			sa[hc2[s[lms[i]]]]=lms[i];
		}
		for(int i = 0;i<n;i++) {
			if(sa[i]>0&&h[sa[i]-1]==1){
				sa[hc1[s[sa[i]-1]]]=sa[i]-1;
				hc1[s[sa[i]-1]]++;
			}
		}
		for(int i = n-1;i>=0;i--) {
			if(sa[i]>0&&h[sa[i]-1]!=1){
				hc3[s[sa[i]-1]]--;
				sa[hc3[s[sa[i]-1]]]=sa[i]-1;
			}
		}
		int[] sl=new int[lm];
		for(int i = 0,j=lm-1;i<n&&j>=0;i++) {
			if(h[sa[i]]==2){sl[j]=sa[i];j--;}
		}
		int[] lmsz=new int[n];
		bool jf=false;
		int ki=0;
		for(int i = lm-2;i>=0;i--) {
			if(sub[sl[i]]==sub[sl[i+1]]){
				bool bo=false;
				for(int j = 0;j<sub[sl[i]];j++) {if(s[sl[i]+j]!=s[sl[i+1]+j]){bo=true;break;}}
				if(bo){ki++;}
				else{jf=true;}
			}
			else{ki++;}
			lmsz[sl[i]]=ki;
		}
		int[] lmsc=new int[lm];
		for(int i = lm-1;i>=0;i--) {lmsc[lm-i-1]=lmsz[lms[i]];}
		hc2[0]=hc3[0]=hi[0];
		hc1[0]=0;
		for(int i = 1;i<m;i++) {
			hc2[i]=hc3[i]=hi[i];
			hc1[i]=hi[i-1];
		}
		if(jf){
			sl=Fu2(lmsc,ki+1);
			for(int i = 0;i<lm;i++) {sl[i]=lms[lm-1-sl[i]];}
		}
		sa=new int[n];
		for(int i = 0;i<lm;i++) {
			hc2[s[sl[i]]]--;
			sa[n-1-hc2[s[sl[i]]]]=sl[i];
		}
		for(int i = n-1;i>=0;i--) {
			if(sa[i]>0&&h[sa[i]-1]==1){
				sa[n-1-hc1[s[sa[i]-1]]]=sa[i]-1;
				hc1[s[sa[i]-1]]++;
			}
		}
		for(int i = 0;i<n;i++) {
			if(sa[i]>0&&h[sa[i]-1]!=1){
				hc3[s[sa[i]-1]]--;
				sa[n-1-hc3[s[sa[i]-1]]]=sa[i]-1;
			}
		}
		return sa;
	}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}