我用的是指针,跑的还挺快的,但是不知道为什么最后几个点会莫名其妙的就RE了。
#include <bits/stdc++.h>
const int N = 2e6;
int n, m; char str[N];
struct node {
node *ch[28];
int v, end;
node (int v = 0) : v(v) {
memset(ch, 0, sizeof(ch));
}
};
node *root = new node(114514);
void insert(char *str)
{
int n = strlen(str + 1);
auto now = root;
for (int i = 1; i <= n; i ++ )
{
int x = str[i] - 'a';
if (now->ch[x] == nullptr) now->ch[x] = new node(x);
now = now->ch[x];
}
now->end ++ ;
}
int f[N];
int query(char *str, int st, int ed)
{
auto now = root;
for (int i = st; i <= ed; i ++ )
{
int x = str[i] - 'a';
if (now->ch[x] == nullptr) return 0;
now = now->ch[x];
if (now->end >= 1)
f[i] = 1;
}
return now->end;
}
auto main() -> signed
{
std::cin.tie(nullptr) -> sync_with_stdio(false);
std::cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
std::cin >> (str + 1);
insert(str);
}
f[0] = 1;
while (m -- )
{
std::cin >> (str + 1);
int length = strlen(str + 1);
for (int i = 1; i <= length; i ++ ) {
f[i] = 0;
}
for (int i = 0; i < length; i ++ ) {
if (f[i]) query(str, i + 1, length);
}
for (int i = length; i >= 0; i -- )
if (f[i]) {
std::cout << i << std::endl;
break;
}
}
return 0;
}