總結
用數組模擬,感覺更直觀,代碼更簡潔吧
題目鏈接
//#pragma GCC optimize(2)
#include<bits/stdc++.h>
//typedef long long ll;
#define ull unsigned long long
#define int long long
#define F first
#define S second
#define endl "\n"//<<flush
#define eps 1e-6
#define base 131
#define lowbit(x) (x&(-x))
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
#define MAXN 0x7fffffff
#define INF 0x3f3f3f3f3f3f3f3f
#define ferma(a,b) pow(a,b-2)
#define pb push_back
#define all(x) x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void file()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:/LSNU/codeforces/duipai/data.txt","r",stdin);
// freopen("D:/LSNU/codeforces/duipai/WA.txt","w",stdout);
#endif
}
const int N=1e6+5;
int son[N][26],cnt[N],pos;
void insert(string &str)
{
int p=0;
for(auto it:str)
{
int &temp=son[p][it-'a'];
if(!temp)
temp=++pos;
p=temp;
}
cnt[p]++;
}
int query(string &str)
{
int sum=0,p=0;
for(auto it:str)
{
int &temp=son[p][it-'a'];
if(!temp)
break;
p=temp;
sum+=cnt[p];
}
return sum;
}
signed main()
{
IOS;
// file();
int n,m;
cin>>n>>m;
while(n--)
{
string str;
cin>>str;
insert(str);
}
while(m--)
{
string str;
cin>>str;
cout<<query(str)<<endl;
}
return 0;
}