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)

#include<bits/stdc++.h>
#define S 2500005
#define N 1005
using namespace std;
typedef pair<int,int> P;
struct d{
  int a,b,c;
  d(){}
  d(int A,int B,int C){
    a=A,b=B,c=C;
  };
  bool operator<(const d&r)const{
    if(a!=r.a)return a<r.a;
    return b<r.b;
  }
};

int n,slen,tlen,r[S];
char s[S],t[N];
d ran[S];
P a[S];

void init(){

  for(int j=0;j<slen;j++)r[j]=s[j];

  int x=1;
  r[slen]=0;
  while(1){
    for(int i=0;i<slen;i++){
      int o=i+x;
      if(o>slen)o=slen;
      ran[i]=d(r[i],r[o],i);
    }
    sort(ran,ran+slen);
    int cnt=0;
    for(int i=0;i<slen;i++){
      r[ran[i].c]=cnt;
      if(ran[i].a==ran[i+1].a&&ran[i].b==ran[i+1].b)
	continue;
      cnt++;
    }
    if(x>slen)break;
    x*=2;
  }
  for(int i=0;i<slen;i++)a[i]=P(r[i],i);
  sort(a,a+slen);
}

bool bynary_search(){
  int L=0,R=slen;
  while(L<R){
    int M=(L+R)/2;
    if(strncmp(s+a[M].second,t,tlen)<0)L=M+1;
    else R=M;
  }
  if(!strncmp(s+a[L].second,t,tlen))return true;
  return false;
}

int main(){
  scanf("%s%d",s,&n);
  slen=strlen(s);
  init();  
  while(n--){
    scanf("%s",t);
    tlen=strlen(t);
    if(bynary_search())printf("1\n");
    else printf("0\n");
  }
  return 0;
}