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++11 (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000

// KMP?????? O(N)
vector<int> build_kmp(const string &s){
  int n = s.size();
  vector<int> a(n+1, -1);
  int j = -1;
  rep(i,n){
    while(j>=0 && s[i]!=s[j]) j = a[j];
    j++;
    if(s[i+1]==s[j]) a[i+1] = a[j];
    else a[i+1]=j;
  }
  return a;
}

bool kmp_match(const string &txt, const string &pat, const vector<int> &skip){
  int n = txt.size();
  int m = pat.size();
  vector<int> ret;
  int j = 0;
  rep(i,n){
    while(j>-1 && txt[i] != pat[j]) j = skip[j];
    j++;
    if(j==m){
      return true;
    }
  }
  return false;
}

int main(){
  string t;
  int q;
  cin>>t>>q;

  rep(_,q){
    string p;
    cin>>p;
    auto skip = build_kmp(p);
    cout << kmp_match(t, p, skip) << endl;
  }

  return 0;
}