#include #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int) (x).size() #define pb push_back #define fi first #define se second using namespace std; typedef long long LL; typedef vector VI; typedef pair PII; typedef long double LD; struct aho { struct out_node { string keyword; out_node * next; out_node(string k, out_node * n) : keyword(k), next(n){} }; struct go_node{ map next; out_node * out; go_node * fail; go_node() {out = NULL; fail = NULL; } } ; go_node * go; aho(vector keywords) { go = new go_node(); for(auto & k: keywords) { go_node * cur = go; for(auto & c : k) cur = cur->next.find(c) != cur->next.end() ? cur->next[c] : (cur->next[c] = new go_node()); cur->out = new out_node(k, cur->out); } queue q; for(auto & a : go->next) q.push(a.second); while(SZ(q)) { go_node * r = q.front(); q.pop(); for(auto & a : r->next) { go_node * s = a.second; q.push(s); go_node * st = r->fail; while(st && st->next.find(a.first) == st->next.end()) st = st->fail; if(!st) st = go; s->fail = st->next[a.first]; if(s->fail) { if(!s->out) s->out = s->fail->out; else { out_node * out = s->out; while(out->next) out = out->next; out->next = s->fail->out; } } } } } vector rob(string s) { go_node * cur = go; int mom = 0; vector res; for(auto & c: s) { mom++; while(cur && cur->next.find(c) == cur->next.end()) cur = cur->fail; if(!cur) cur = go; cur = cur->next[c]; if(!cur) cur = go; for(out_node * out = cur->out;out;out = out->next) { res.push_back(mom); break; } } return res; } }; map > mapa; const int inf = 1e9 + 7; const int maxn = 1e6 + 7; int gdzie[maxn]; const int pot = 1<<20; int tree[2 * pot + 7]; void add(int l, int p, int val) { l += pot; p += pot; while(l <= p) { if(l & 1) tree[l] = min(tree[l], val); if(!(p & 1)) tree[p] = min(tree[p], val); l = (l + 1) / 2; p = (p - 1) / 2; } } int val(int x) { x += pot; int res = inf; while(x > 0) { res = min(res, tree[x]); x /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for(int i = 0 ;i < 2 * pot;i++) tree[i] = inf; int n; cin>>n; string wzo; cin>>wzo; for(int i = 1;i <= n;i++) { string a; cin>>a; mapa[SZ(a)].pb(a); } for(int i = 1;i <= SZ(wzo);i++) if(SZ(mapa[i])) { aho cnt(mapa[i]); auto xd = cnt.rob(wzo); for(auto s : xd) gdzie[s - i + 1] = max(gdzie[s - i + 1], s + 1); } //for(int i = 1;i <= SZ(wzo);i++) // cerr< SZ(wzo) * 2) cout<<-1; else cout<