HDU1251 统计难题 trie树 简单

时间:2022-04-14 20:02:15

题意: 找前缀数量 裸模板

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
const double eps=1e-;
const long long modn=;
struct tri{
int count;
int next[];
bool exist;
}e[maxn];
char a[]={};
int tot=;
void doit(int x,int k,int j){
e[x].count++;
if(k<j){
e[x].exist=;
return;
}
int w=a[j]-'a';
if(!e[x].next[w]){
e[x].next[w]=++tot;
doit(tot,k,j+);
}else{
doit(e[x].next[w],k,j+);
}
}
int getit(int x,int k,int j){
if(k<j){
return e[x].count;
}
int w=a[j]-'a';
if(!e[x].next[w]){
return ;
}
else{
return getit(e[x].next[w],k,j+);
}
}
int main(){
memset(e,,sizeof(e));
a[]=getchar();
while(){
int i=;a[i]=getchar();
while(a[i]!='\n'){
i++;
a[i]=getchar();
}
doit(,i-,);
a[]=getchar();
if(a[]=='\n'){
break;
}
}
while(~scanf("%s",&a)){
int z=strlen(a)-;
printf("%d\n",getit(,z,));
}
return ;
}