思路:
直接暴力
把t丢进tire,然后s进去找就行了
c o d e code code
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n, m;
int t[3000010][3], tot, a[3000010];
vector <int> q[3000010];
void tire(string s, int j) {
int u = 0;
for(int i = 0; i < s.size(); i ++) {
if(!t[u][s[i] - 'a'])
t[u][s[i] - 'a'] = ++ tot;
u = t[u][s[i] - 'a'];
q[u].push_back(j);
if(q[u].size() == 1) a[u] = j - 1;
else if(q[u].size() >= 2) a[u] = max(a[u], j - q[u][q[u].size() - 2] - 1);
}
}
int ask(string s) {
int u = 0;
for(int i = 0; i < s.size(); i ++) {
if(!t[u][s[i] - 'a'])
return n;
u = t[u][s[i] - 'a'];
}
return max(a[u], n - q[u][q[u].size() - 1]);
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) {
string s;
cin>>s;
tire(s, i);
}
while(m --) {
string s;
cin>>s;
printf("%d\n", ask(s));
}
return 0;
}