trie字典树

时间:2022-02-06 15:59:28

---恢复内容开始---

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
const int N=1e6+;
const int mod=;
void read(int &a)
{
a=;
int d=;
char ch;
while(ch=getchar(),ch>''||ch<'')
if(ch=='-')
d=-;
a=ch-'';
while(ch=getchar(),ch>=''&&ch<='')
a=a*+ch-'';
a*=d;
}
void write(int x)
{
if(x<)
putchar(),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
int trie[][],len,root,tot,sum[];
bool p;
char s[];
void ins()
{
len=strlen(s);
root=;
for(re int i=;i<len;i++)
{
int id=s[i]-;
if(!trie[root][id])
trie[root][id]=++tot;
root=trie[root][id];
sum[root]++;
}
}
int sear()
{
root=;
len=strlen(s);
for(re int i=;i<len;i++)
{
int id=s[i]-;
if(!trie[root][id])
return ;
root=trie[root][id];
}
return sum[root];
}
int main()
{
int f=;
while(gets(s))
{
if(strlen(s)==)
{
f=;
continue;
}
if(!f)
ins();
else
cout<<sear()<<endl;
}
return ;
}

---恢复内容结束---