#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
vector<int> vx;
inline int mp(int x) {return upper_bound(vx.begin(),vx.end(),x)-vx.begin();}
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
const int N = 1e6+10;
int idx;
int ne[N][26],isend[N];
void insert(string &s)
{
int p = 0;
int n = s.size();
for(int i=0;i<n;++i)
{
int x = s[i]-'a';
if(!ne[p][x]) ne[p][x] = ++idx, p = idx;
else p = ne[p][x];
}
isend[p]++;
}
int query(string &s)
{
int p = 0;
int n = s.size();
int sum = 0;
for(int i=0;i<n;++i)
{
int x = s[i]-'a';
if(!ne[p][x]) return sum;
else p = ne[p][x], sum+=isend[p];
}
return sum;
}
void solve()
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;++i)
{
string s;
cin>>s;
insert(s);
}
for(int i=0;i<m;++i)
{
string s;
cin>>s;
cout<<query(s)<<'\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
//cin>>T;
while(T--)
{
solve();
}
}
标签:typedef,return,前缀,P10470,trie,long,int,vector,vx
From: https://www.cnblogs.com/ruoye123456/p/18413456