** 2381 - 三年二班的投票 ---Trie字典树模板
** 来源:东方博弈oj oj.czos.cn
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
char s[N];
int cnt[N];
int ch[N][26],idx=0;
int n,m;
void insertt(char s[])
{
int p=0;
for(int i=0;s[i]!='\0';i++)
{
int x=s[i]-'a';
if(!ch[p][x]) ch[p][x]=++idx;
p=ch[p][x];
}
cnt[p]++;
}
int query(char s[])
{
int p=0;
for(int i=0;s[i]!='\0';i++)
{
int x=s[i]-'a';
if(!ch[p][x]) return 0;
p=ch[p][x];
}
return cnt[p];
}
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
insertt(s);
}
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
printf("%d\n",query(s));
}
return 0;
}
Trie字典树+ 2381 - 三年二班的投票 ---Trie字典树模板
最新推荐文章于 2025-03-17 17:58:34 发布