1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <cstdio>
using namespace std;
using ll = long long;
struct suffix_array {
string s;
int n;
vector<int> c, p;
suffix_array(string s) {
s += '$';
this->s = s;
this->n = s.length();
int n = s.length();
vector<int> c(n, 0), p(n, 0), cnt(max(n, 256), 0);
for (char x : s)
cnt[x]++;
for (int i = 1; i < 256; i++)
cnt[i] += cnt[i - 1];
for (int i = 0; i < n; i++)
p[--cnt[s[i]]] = i;
c[p[0]] = 0;
for (int i = 1; i < n; i++)
c[p[i]] = (s[p[i]] != s[p[i - 1]]) ? c[p[i - 1]] + 1 : c[p[i - 1]];
for (int i = 2; i <= 2 * n; i <<= 1) {
vector<pair<int, int>> v;
vector<int> pn = p, cn = c;
for (int j = 0; j < n; j++) {
v.push_back({ j, (j + i / 2) % n });
}
for (int& j : cnt)
j = 0;
for (int j = 0; j < n; j++)
cnt[c[v[j].second]]++;
for (int j = 1; j < n; j++)
cnt[j] += cnt[j - 1];
for (int j = 0; j < n; j++)
pn[--cnt[c[v[j].second]]] = j;
for (int& j : cnt)
j = 0;
for (int j = 0; j < n; j++)
cnt[c[pn[j]]]++;
for (int j = 1; j < n; j++)
cnt[j] += cnt[j - 1];
for (int j = 0; j < n; j++)
p[--cnt[c[pn[n - 1 - j]]]] = pn[n - 1 - j];
cn[p[0]] = 0;
int classes = 1;
for (int j = 1; j < n; j++) {
pair<int, int> cur = { c[p[j]], c[(p[j] + i / 2) % n] };
pair<int, int> prev = { c[p[j - 1]], c[(p[j - 1] + i / 2 + n) % n] };
if (cur != prev)
++classes;
cn[p[j]] = classes - 1;
}
c.swap(cn);
}
// c.pop_back();
// for(int &x : c) x--;
// p.erase(p.begin());
this->p = p;
this->c = c;
}
void prt() {
for (int x : p)
cout << x << " ";
cout << endl;
}
int first_occ(string x) {
int l = 0, r = n - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
string sub = s.substr(p[mid], min(s.length() - p[mid], x.length()));
if (sub >= x) {
if (sub == x)
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
bool exist(string x) {
int l = 0, r = n - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
string sub1 = s.substr(p[mid], s.length() - p[mid]);
string sub = sub1.substr(0, min(sub1.length(), x.length()));
if (sub >= x) {
if (sub == x)
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return (ans != -1);
}
};
char buf[300010];
string read() {
scanf("%s", buf);
return buf;
}
int main() {
string s = read();
suffix_array suf(s);
int q;
scanf("%d", &q);
while (q--) {
string x = read();
puts(suf.exist(x) ? "Yes" : "No");
}
}
| |