#include<bits/stdc++.h> #define STC static usingnamespace std; constint N = 1.1e6 + 5; namespace SAM { int tot = 1, last = 1; structNode { int len, fa; int ch[3]; } nd[N << 1]; intget_v(char ch) { return ch == '$' ? 2 : ch - '0'; } voidins(char ch) { int x = get_v(ch); int p = last, np = last = ++tot; for (; p && !nd[p].ch[x]; p = nd[p].fa) nd[p].ch[x] = np; if (!p) nd[np].fa = 1; else { int q = nd[p].ch[x]; if (nd[q].len == nd[p].len + 1) nd[np].fa = q; else { int nq = ++tot; nd[nq] = nd[q], nd[nq].len = nd[p].len + 1; nd[q].fa = nd[np].fa = nq; for (; p && nd[p].ch[x] == q; p = nd[p].fa) nd[p].ch[x] = nq; } } } voidfind(char *s, int x[]) { int p = 1, now = 0, t, len = strlen(s + 1); for (int i = 1; i <= len; ++i) { t = get_v(s[i]); if (nd[p].ch[t]) ++now, p = nd[p].ch[t]; else { for (; p && !nd[p].ch[t]; p = nd[p].fa); if (!p) p = 1, now = 0; else now = nd[p].len + 1, p = nd[p].ch[t]; } x[i] = now; } } } int len[N]; int f[N]; boolcheck(int x, int sl)//x就是L0 { STC int q[N]; int l = 1, r = 0; for (int i = 0; i < x; ++i) f[i] = 0; for (int i = x; i <= sl; ++i) { //由于i-L0可以跟新i,所以要先入队 while (l <= r && f[q[r]] - q[r] <= f[i - x] - (i - x)) --r; q[++r] = i - x; f[i] = f[i - 1]; while (l <= r && q[l] < i - len[i]) ++l; if (l <= r) f[i] = max(f[i], f[q[l]] + i - q[l]); } return f[sl] * 10 >= sl * 9; } intwork(char *s) { SAM::find(s, len); int sl = strlen(s + 1); int l = 0, r = sl, mid, res = 0; while (l <= r) { mid = l + r >> 1; if (check(mid, sl)) { res = mid; l = mid + 1; } else r = mid - 1; } return res; } intmain() { int n, m; STC char s[N]; scanf("%d%d", &n, &m); for (int i = 1, t; i <= m; ++i) { scanf("%s", s + 1); t = strlen(s + 1); for (int j = 1; j <= t; ++j) SAM::ins(s[j]); SAM::ins('$'); } for (int i = 1; i <= n; ++i) { scanf("%s", s + 1); printf("%d\n", work(s)); } return0; }