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

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class T>
istream& operator >> (istream& is, vector<T>& v)
{
	for (auto &i : v) is >> i;
	return is;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& v)
{
	const string delimiter = "\n";
	REP(i, v.size())
	{
		os << v[i];
		if (i != v.size() - 1) os << delimiter;
	}
	return os;
}
/*--------------------template--------------------*/

typedef vector<int> SuffixArray;
int n, k;
vi rnk, tmp;

bool comp_sa(const int i, const int j)
{
	if (rnk[i] != rnk[j]) return rnk[i] < rnk[j];
	else
	{
		int ri = i + k <= n ? rnk[i + k] : -1;
		int rj = j + k <= n ? rnk[j + k] : -1;
		return ri < rj;
	}
}

SuffixArray construct_sa(const string& s)
{
	n = s.size();
	SuffixArray sa(n + 1);
	rnk.resize(n + 1), tmp.resize(n + 1);
	REP(i, n + 1)
	{
		sa[i] = i;
		if (i == n) rnk[i] = -1;
		else rnk[i] = s[i];
	}
	for (k = 1; k <= n; k *= 2)
	{
		sort(ALL(sa), comp_sa);
		tmp[sa[0]] = 0;
		REP(i, n) tmp[sa[i + 1]] = tmp[sa[i]] + (comp_sa(sa[i], sa[i + 1]) ? 1 : 0);
		rnk = tmp;
	}
	return sa;
}

bool search(const string& s, const string& t, const SuffixArray& sa)
{
	int l = 0, r = s.size();
	while (r - l > 1)
	{
		int mid = (r + l) / 2;
		if (s.compare(sa[mid], t.size(), t) < 0) l = mid;
		else r = mid;
	}
	return s.compare(sa[r], t.size(), t) == 0;
}

int lower_bound(const string& s, const string& t, const SuffixArray& sa)
{
	int l = 0, r = s.size() + 1;
	while (r - l > 1)
	{
		int mid = (r + l) / 2;
		if (s.compare(sa[mid], t.size(), t) < 0) l = mid;
		else r = mid;
	}
	return r;
}

int upper_bound(const string& s, const string& t, const SuffixArray& sa)
{
	int l = 0, r = s.size() + 1;
	while (r - l > 1)
	{
		int mid = (r + l) / 2;
		if (s.compare(sa[mid], t.size(), t) <= 0) l = mid;
		else r = mid;
	}
	return r;
}


int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	string s; cin >> s;
	int q; cin >> q;
	SuffixArray sa = construct_sa(s);
	while (q--)
	{
		string t; cin >> t;
		if (search(s, t, sa)) cout << 1 << endl;
		else cout << 0 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 1060
memory_kb: 15752
final_rank: 2

code_snippet: // competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_14_D
#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
namespace internal {
std::vector<int> sa_naive(const std::vector<int> &s) {
    int n = int(s.size());
    std::vector<int> sa(n);
    std::iota(sa.begin(), sa.end(), 0);
    std::sort(sa.begin(), sa.end(), [&](int l, int r) {
        if (l == r) return false;
        while (l < n && r < n) {
            if (s[l] != s[r]) return s[l] < s[r];
            ++l, ++r;
        }
        return l == n;
    });
    return sa;
}
std::vector<int> sa_doubling(const std::vector<int> &s) {
    int n = int(s.size());
    std::vector<int> sa(n), rnk = s, tmp(n);
    std::iota(sa.begin(), sa.end(), 0);
    for (int k = 1; k < n; k *= 2) {
        auto cmp = [&](int x, int y) {
            if (rnk[x] != rnk[y]) return rnk[x] < rnk[y];
            int rx = x + k < n ? rnk[x + k] : -1;
            int ry = y + k < n ? rnk[y + k] : -1;
            return rx < ry;
        };
        std::sort(sa.begin(), sa.end(), cmp);
        tmp[sa[0]] = 0;
        for (int i = 1; i < n; ++i) tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0);
        std::swap(tmp, rnk);
    }
    return sa;
}
template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40>
std::vector<int> sa_is(const std::vector<int> &s, int upper) {
    int n = int(s.size());
    if (n == 0) return {};
    if (n == 1) return {0};
    if (n == 2) {
        if (s[0] < s[1]) return {0, 1};
        else return {1, 0};
    }
    if (n < THRESHOLD_NAIVE) return sa_naive(s);
    if (n < THRESHOLD_DOUBLING) return sa_doubling(s);
    std::vector<int> sa(n);
    std::vector<bool> ls(n);
    for (int i = n - 2; i >= 0; i--) ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]);
    std::vector<int> sum_l(upper + 1), sum_s(upper + 1);
    for (int i = 0; i < n; ++i) {
        if (!ls[i]) ++sum_s[s[i]];
        else ++sum_l[s[i] + 1];
    }
    for (int i = 0; i <= upper; ++i) {
        sum_s[i] += sum_l[i];
        if (i < upper) sum_l[i + 1] += sum_s[i];
    }
    auto induce = [&](const std::vector<int> &lms) {
        std::fill(sa.begin(), sa.end(), -1);
        std::vector<int> buf(upper + 1);
        std::copy(sum_s.begin(), sum_s.end(), buf.begin());
        for (auto d : lms) {
            if (d == n) continue;
            sa[buf[s[d]]++] = d;
        }
        std::copy(sum_l.begin(), sum_l.end(), buf.begin());
        sa[buf[s[n - 1]]++] = n - 1;
        for (int i = 0; i < n; ++i) {
            int v = sa[i];
            if (v >= 1 && !ls[v - 1]) sa[buf[s[v - 1]]++] = v - 1;
        }
        std::copy(sum_l.begin(), sum_l.end(), buf.begin());
        for (int i = n - 1; i >= 0; i--) {
            int v = sa[i];
            if (v >= 1 && ls[v - 1]) sa[--buf[s[v - 1] + 1]] = v - 1;
        }
    };
    std::vector<int> lms_map(n + 1, -1);
    int m = 0;
    for (int i = 1; i < n; ++i) {
        if (!ls[i - 1] && ls[i]) lms_map[i] = m++;
    }
    std::vector<int> lms;
    lms.reserve(m);
    for (int i = 1; i < n; ++i) {
        if (!ls[i - 1] && ls[i]) lms.emplace_back(i);
    }
    induce(lms);
    if (m) {
        std::vector<int> sorted_lms;
        sorted_lms.reserve(m);
        for (int v : sa) {
            if (lms_map[v] != -1) sorted_lms.emplace_back(v);
        }
        std::vector<int> rec_s(m);
        int rec_upper = 0;
        rec_s[lms_map[sorted_lms[0]]] = 0;
        for (int i = 1; i < m; ++i) {
            int l = sorted_lms[i - 1], r = sorted_lms[i];
            int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n;
            int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n;
            bool same = true;
            if (end_l - l != end_r - r) {
                same = false;
            } else {
                while (l < end_l) {
                    if (s[l] != s[r]) break;
                    ++l, ++r;
                }
                if (l == n || s[l] != s[r]) same = false;
            }
            if (!same) ++rec_upper;
            rec_s[lms_map[sorted_lms[i]]] = rec_upper;
        }
        auto rec_sa = sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper);
        for (int i = 0; i < m; ++i) sorted_lms[i] = lms[rec_sa[i]];
        induce(sorted_lms);
    }
    return sa;
}
}  // namespace internal
/**
 * @brief Suffix Array
 *
 * @param s 配列
 * @param upper 最大値
 * @return std::vector<int>
 */
std::vector<int> suffix_array(const std::vector<int> &s, int upper) {
    assert(0 <= upper);
    for (int d : s) assert(0 <= d && d <= upper);
    auto sa = internal::sa_is(s, upper);
    return sa;
}
/**
 * @brief Suffix Array
 *
 * @tparam T 配列の型
 * @param s 配列
 * @retval std::vector<int> Suffix Array
 */
template <class T>
std::vector<int> suffix_array(const std::vector<T> &s) {
    int n = int(s.size());
    std::vector<int> idx(n);
    std::iota(idx.begin(), idx.end(), 0);
    std::sort(idx.begin(), idx.end(), [&](int l, int r) { return s[l] < s[r]; });
    std::vector<int> s2(n);
    int now = 0;
    for (int i = 0; i < n; ++i) {
        if (i && s[idx[i - 1]] != s[idx[i]]) ++now;
        s2[idx[i]] = now;
    }
    return internal::sa_is(s2, now);
}
/**
 * @brief Suffix Array
 *
 * @param s 文字列
 * @retval std::vector<int> Suffix Array
 */
std::vector<int> suffix_array(const std::string &s) {
    int n = int(s.size());
    std::vector<int> s2(n);
    for (int i = 0; i < n; ++i) s2[i] = s[i];
    return internal::sa_is(s2, 255);
}
template <class T>
std::vector<int> lcp_array(const std::vector<T> &s, const std::vector<int> &sa) {
    int n = int(s.size());
    assert(n >= 1);
    std::vector<int> rnk(n);
    for (int i = 0; i < n; ++i) { rnk[sa[i]] = i; }
    std::vector<int> lcp(n - 1);
    int h = 0;
    for (int i = 0; i < n; ++i) {
        if (h > 0) --h;
        if (rnk[i] == 0) continue;
        int j = sa[rnk[i] - 1];
        for (; j + h < n && i + h < n; ++h) {
            if (s[j + h] != s[i + h]) break;
        }
        lcp[rnk[i] - 1] = h;
    }
    return lcp;
}
std::vector<int> lcp_array(const std::string &s, const std::vector<int> &sa) {
    int n = int(s.size());
    std::vector<int> s2(n);
    for (int i = 0; i < n; ++i) { s2[i] = s[i]; }
    return lcp_array(s2, sa);
}
int main(void) {
    std::string s;
    std::cin >> s;
    int n = s.size();
    auto v = suffix_array(s);
    int q;
    std::cin >> q;
    while (q--) {
        std::string t;
        std::cin >> t;
        int l = -1, r = n - 1;
        while (r - l > 1) {
            int m = (l + r) / 2;
            if (s.substr(v[m], t.size()) < t)
                l = m;
            else
                r = m;
        }
        std::cout << (s.substr(v[r], t.size()) == t) << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 28668
final_rank: 1

code_snippet: // competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_14_D
#include <iostream>
#include <string>
#include <cmath>
#include <cstdint>
#include <numeric>
/// @brief めぐる式二分探索
template <class T, class F>
std::int64_t meguru_binary_search(T ok, T ng, F check) {
    while (std::abs(ok - ng) > 1) {
        T mid = std::midpoint(ok, ng);
        (check(mid) ? ok : ng) = mid;
    }
    return ok;
}
#include <array>
#include <bit>
#include <cassert>
#include <vector>
/// @brief スパーステーブル
template <class M>
struct sparse_table {
  private:
    using T = typename M::value_type;
  public:
    sparse_table() = default;
    sparse_table(const std::vector<T> &v) : _size(v.size()), data() {
        int b = std::max(1, std::countr_zero(std::bit_ceil<unsigned>(_size)));
        data.emplace_back(v);
        for (int i = 1; i < b; ++i) data.emplace_back(_size + 1 - (1 << i));
        for (int i = 1; i < b; ++i) {
            for (int j = 0; j + (1 << i) <= _size; ++j) {
                data[i][j] = M::op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _size);
        if (l == r) return M::id();
        if (l + 1 == r) return data[0][l];
        int b = 31 - std::countl_zero<unsigned>(r - l - 1);
        return M::op(data[b][l], data[b][r - (1 << b)]);
    }
  private:
    int _size;
    std::vector<std::vector<T>> data;
};
namespace internal {
template <class T, int N>
struct fixed_stack {
    constexpr fixed_stack() : _size(), _data() {}
    constexpr T top() const { return _data[_size - 1]; }
    constexpr bool empty() const { return _size == 0; }
    constexpr int size() const { return _size; }
    constexpr void emplace(const T &e) { _data[_size++] = e; }
    constexpr void emplace(T &&e) { _data[_size++] = e; }
    constexpr void pop() { --_size; }
    constexpr void clear() { _size = 0; }
  private:
    int _size;
    std::array<T, N> _data;
};
}  // namespace internal
/**
 * @brief 線形 Sparse Table
 *
 * @tparam M
 */
template <class M>
struct linear_sparse_table {
  private:
    using T = M::value_type;
    static constexpr int W = 64;
  public:
    linear_sparse_table() = default;
    linear_sparse_table(const std::vector<T> &v) : _size(v.size()), data(v) {
        int n = v.size();
        int b = n / W;
        internal::fixed_stack<int, W + 1> st;
        std::vector<T> u(b);
        word_data.resize(b + (n > b * W));
        for (int i = 0; i < b; ++i) {
            T m = M::id();
            std::uint64_t bit = 0;
            std::vector<std::uint64_t> bits(W);
            for (int j = 0; j < W; ++j) {
                m = M::op(m, v[i * W + j]);
                while (!st.empty() && M::op(v[i * W + st.top()], v[i * W + j]) == v[i * W + j]) {
                    bit ^= std::uint64_t(1) << st.top();
                    st.pop();
                }
                bits[j] = bit;
                bit |= std::uint64_t(1) << j;
                st.emplace(j);
            }
            u[i] = m;
            word_data[i] = bits;
            st.clear();
        }
        if (n > b * W) {
            std::uint64_t bit = 0;
            std::vector<std::uint64_t> bits(n - b * W);
            for (int j = 0; j < n - b * W; ++j) {
                while (!st.empty() && M::op(v[b * W + st.top()], v[b * W + j]) == v[b * W + j]) {
                    bit ^= std::uint64_t(1) << st.top();
                    st.pop();
                }
                bits[j] = bit;
                bit |= std::uint64_t(1) << j;
                st.emplace(j);
            }
            word_data[b] = bits;
        }
        block_table = sparse_table<M>(u);
    }
    const T &operator[](int k) const { return data[k]; }
    T prod(int l, int r) const {
        assert(0 <= l && l < r && r <= _size);
        int lb = (l + W - 1) / W, rb = r / W;
        if (lb > rb) return word_prod(l, r);
        T res = (lb == rb ? M::id() : block_table.prod(lb, rb));
        if (l < lb * W) res = M::op(res, word_prod(l, lb * W));
        if (rb * W < r) res = M::op(res, word_prod(rb * W, r));
        return res;
    }
  private:
    int _size;
    std::vector<T> data;
    sparse_table<M> block_table;
    std::vector<std::vector<std::uint64_t>> word_data;
    T word_prod(int l, int r) const {
        if (l == r) return M::id();
        int b = l / W;
        int lw = l - b * W, rw = r - b * W;
        if ((word_data[b][rw - 1] >> lw) == 0ul) return data[r - 1];
        return data[l + std::countr_zero(word_data[b][rw - 1] >> lw)];
    }
};
#include <algorithm>
#include <limits>
#include <utility>
template <class T>
struct Add {
    using value_type = T;
    static constexpr T id() { return T(); }
    static constexpr T op(const T &lhs, const T &rhs) { return lhs + rhs; }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs + rhs;
    }
};
template <class T>
struct Mul {
    using value_type = T;
    static constexpr T id() { return T(1); }
    static constexpr T op(const T &lhs, const T &rhs) { return lhs * rhs; }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs * rhs;
    }
};
template <class T>
struct And {
    using value_type = T;
    static constexpr T id() { return std::numeric_limits<T>::max(); }
    static constexpr T op(const T &lhs, const T &rhs) { return lhs & rhs; }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs & rhs;
    }
};
template <class T>
struct Or {
    using value_type = T;
    static constexpr T id() { return T(); }
    static constexpr T op(const T &lhs, const T &rhs) { return lhs | rhs; }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs | rhs;
    }
};
template <class T>
struct Xor {
    using value_type = T;
    static constexpr T id() { return T(); }
    static constexpr T op(const T &lhs, const T &rhs) { return lhs ^ rhs; }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs ^ rhs;
    }
};
template <class T>
struct Min {
    using value_type = T;
    static constexpr T id() { return std::numeric_limits<T>::max(); }
    static constexpr T op(const T &lhs, const T &rhs) { return std::min(lhs, rhs); }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return std::min((U)lhs, rhs);
    }
};
template <class T>
struct Max {
    using value_type = T;
    static constexpr T id() { return std::numeric_limits<T>::lowest(); }
    static constexpr T op(const T &lhs, const T &rhs) { return std::max(lhs, rhs); }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return std::max((U)lhs, rhs);
    }
};
template <class T>
struct Gcd {
    using value_type = T;
    static constexpr T id() { return std::numeric_limits<T>::max(); }
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs == Gcd::id() ? rhs : (rhs == Gcd::id() ? lhs : std::gcd(lhs, rhs));
    }
};
template <class T>
struct Lcm {
    using value_type = T;
    static constexpr T id() { return std::numeric_limits<T>::max(); }
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs == Lcm::id() ? rhs : (rhs == Lcm::id() ? lhs : std::lcm(lhs, rhs));
    }
};
template <class T>
struct Update {
    using value_type = T;
    static constexpr T id() { return std::numeric_limits<T>::max(); }
    static constexpr T op(const T &lhs, const T &rhs) { return lhs == Update::id() ? rhs : lhs; }
    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs == Update::id() ? rhs : lhs;
    }
};
template <class T>
struct Affine {
    using P = std::pair<T, T>;
    using value_type = P;
    static constexpr P id() { return P(1, 0); }
    static constexpr P op(P lhs, P rhs) {
        return {lhs.first * rhs.first, lhs.first * rhs.second + lhs.second};
    }
};
template <class M>
struct Rev {
    using T = typename M::value_type;
    using value_type = T;
    static constexpr T id() { return M::id(); }
    static constexpr T op(T lhs, T rhs) { return M::op(rhs, lhs); }
};
namespace internal {
std::vector<int> sa_naive(const std::vector<int> &s) {
    int n = int(s.size());
    std::vector<int> sa(n);
    std::iota(sa.begin(), sa.end(), 0);
    std::sort(sa.begin(), sa.end(), [&](int l, int r) {
        if (l == r) return false;
        while (l < n && r < n) {
            if (s[l] != s[r]) return s[l] < s[r];
            ++l, ++r;
        }
        return l == n;
    });
    return sa;
}
std::vector<int> sa_doubling(const std::vector<int> &s) {
    int n = int(s.size());
    std::vector<int> sa(n), rnk = s, tmp(n);
    std::iota(sa.begin(), sa.end(), 0);
    for (int k = 1; k < n; k *= 2) {
        auto cmp = [&](int x, int y) {
            if (rnk[x] != rnk[y]) return rnk[x] < rnk[y];
            int rx = x + k < n ? rnk[x + k] : -1;
            int ry = y + k < n ? rnk[y + k] : -1;
            return rx < ry;
        };
        std::sort(sa.begin(), sa.end(), cmp);
        tmp[sa[0]] = 0;
        for (int i = 1; i < n; ++i) tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0);
        std::swap(tmp, rnk);
    }
    return sa;
}
template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40>
std::vector<int> sa_is(const std::vector<int> &s, int upper) {
    int n = int(s.size());
    if (n == 0) return {};
    if (n == 1) return {0};
    if (n == 2) {
        if (s[0] < s[1]) return {0, 1};
        else return {1, 0};
    }
    if (n < THRESHOLD_NAIVE) return sa_naive(s);
    if (n < THRESHOLD_DOUBLING) return sa_doubling(s);
    std::vector<int> sa(n);
    std::vector<bool> ls(n);
    for (int i = n - 2; i >= 0; i--) ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]);
    std::vector<int> sum_l(upper + 1), sum_s(upper + 1);
    for (int i = 0; i < n; ++i) {
        if (!ls[i]) ++sum_s[s[i]];
        else ++sum_l[s[i] + 1];
    }
    for (int i = 0; i <= upper; ++i) {
        sum_s[i] += sum_l[i];
        if (i < upper) sum_l[i + 1] += sum_s[i];
    }
    auto induce = [&](const std::vector<int> &lms) {
        std::fill(sa.begin(), sa.end(), -1);
        std::vector<int> buf(upper + 1);
        std::copy(sum_s.begin(), sum_s.end(), buf.begin());
        for (auto d : lms) {
            if (d == n) continue;
            sa[buf[s[d]]++] = d;
        }
        std::copy(sum_l.begin(), sum_l.end(), buf.begin());
        sa[buf[s[n - 1]]++] = n - 1;
        for (int i = 0; i < n; ++i) {
            int v = sa[i];
            if (v >= 1 && !ls[v - 1]) sa[buf[s[v - 1]]++] = v - 1;
        }
        std::copy(sum_l.begin(), sum_l.end(), buf.begin());
        for (int i = n - 1; i >= 0; i--) {
            int v = sa[i];
            if (v >= 1 && ls[v - 1]) sa[--buf[s[v - 1] + 1]] = v - 1;
        }
    };
    std::vector<int> lms_map(n + 1, -1);
    int m = 0;
    for (int i = 1; i < n; ++i) {
        if (!ls[i - 1] && ls[i]) lms_map[i] = m++;
    }
    std::vector<int> lms;
    lms.reserve(m);
    for (int i = 1; i < n; ++i) {
        if (!ls[i - 1] && ls[i]) lms.emplace_back(i);
    }
    induce(lms);
    if (m) {
        std::vector<int> sorted_lms;
        sorted_lms.reserve(m);
        for (int v : sa) {
            if (lms_map[v] != -1) sorted_lms.emplace_back(v);
        }
        std::vector<int> rec_s(m);
        int rec_upper = 0;
        rec_s[lms_map[sorted_lms[0]]] = 0;
        for (int i = 1; i < m; ++i) {
            int l = sorted_lms[i - 1], r = sorted_lms[i];
            int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n;
            int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n;
            bool same = true;
            if (end_l - l != end_r - r) {
                same = false;
            } else {
                while (l < end_l) {
                    if (s[l] != s[r]) break;
                    ++l, ++r;
                }
                if (l == n || s[l] != s[r]) same = false;
            }
            if (!same) ++rec_upper;
            rec_s[lms_map[sorted_lms[i]]] = rec_upper;
        }
        auto rec_sa = sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper);
        for (int i = 0; i < m; ++i) sorted_lms[i] = lms[rec_sa[i]];
        induce(sorted_lms);
    }
    return sa;
}
}  // namespace internal
/**
 * @brief Suffix Array
 *
 * @param s 配列
 * @param upper 最大値
 * @return std::vector<int>
 */
std::vector<int> suffix_array(const std::vector<int> &s, int upper) {
    assert(0 <= upper);
    for (int d : s) assert(0 <= d && d <= upper);
    auto sa = internal::sa_is(s, upper);
    return sa;
}
/**
 * @brief Suffix Array
 *
 * @tparam T 配列の型
 * @param s 配列
 * @retval std::vector<int> Suffix Array
 */
template <class T>
std::vector<int> suffix_array(const std::vector<T> &s) {
    int n = int(s.size());
    std::vector<int> idx(n);
    std::iota(idx.begin(), idx.end(), 0);
    std::sort(idx.begin(), idx.end(), [&](int l, int r) { return s[l] < s[r]; });
    std::vector<int> s2(n);
    int now = 0;
    for (int i = 0; i < n; ++i) {
        if (i && s[idx[i - 1]] != s[idx[i]]) ++now;
        s2[idx[i]] = now;
    }
    return internal::sa_is(s2, now);
}
/**
 * @brief Suffix Array
 *
 * @param s 文字列
 * @retval std::vector<int> Suffix Array
 */
std::vector<int> suffix_array(const std::string &s) {
    int n = int(s.size());
    std::vector<int> s2(n);
    for (int i = 0; i < n; ++i) s2[i] = s[i];
    return internal::sa_is(s2, 255);
}
template <class T>
std::vector<int> lcp_array(const std::vector<T> &s, const std::vector<int> &sa) {
    int n = int(s.size());
    assert(n >= 1);
    std::vector<int> rnk(n);
    for (int i = 0; i < n; ++i) { rnk[sa[i]] = i; }
    std::vector<int> lcp(n - 1);
    int h = 0;
    for (int i = 0; i < n; ++i) {
        if (h > 0) --h;
        if (rnk[i] == 0) continue;
        int j = sa[rnk[i] - 1];
        for (; j + h < n && i + h < n; ++h) {
            if (s[j + h] != s[i + h]) break;
        }
        lcp[rnk[i] - 1] = h;
    }
    return lcp;
}
std::vector<int> lcp_array(const std::string &s, const std::vector<int> &sa) {
    int n = int(s.size());
    std::vector<int> s2(n);
    for (int i = 0; i < n; ++i) { s2[i] = s[i]; }
    return lcp_array(s2, sa);
}
int main(void) {
    std::string s;
    std::cin >> s;
    int n = s.size();
    auto v = suffix_array(s);
    auto lcp = lcp_array(s, v);
    lcp.emplace(lcp.begin(), 0);
    linear_sparse_table<Min<int>> st(lcp);
    int q;
    std::cin >> q;
    while (q--) {
        std::string t;
        std::cin >> t;
        int l = 0, x = 0;
        auto f = [&](int m) {
            if (st.prod(l, m + 1) > x) {
                l = m + 1;
                return false;
            }
            if (st.prod(l, m + 1) == x) {
                for (int i = x; i < (int)t.size(); ++i) {
                    if (v[m] + i == (int)s.size() || s[v[m] + i] < t[i]) {
                        x = i;
                        l = m + 1;
                        return false;
                    } else if (s[v[m] + i] > t[i]) {
                        return true;
                    }
                }
            }
            return true;
        };
        int ans = meguru_binary_search(n - 1, -1, f);
        std::cout << (s.substr(v[ans], t.size()) == t) << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 29268
final_rank: 3