HDU1251 统计难题 trie树 简单

题意: 找前缀数量 裸模板

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=500010;
 9 const double eps=1e-8;
10 const long long modn=1000;
11 struct tri{
12     int count;
13     int next[26];
14     bool exist;
15 }e[maxn];
16 char a[20]={};
17 int tot=0;
18 void doit(int x,int k,int j){
19     e[x].count++;
20     if(k<j){
21         e[x].exist=1;
22         return;
23     }
24     int w=a[j]-'a';
25     if(!e[x].next[w]){
26         e[x].next[w]=++tot;
27         doit(tot,k,j+1);
28     }else{
29         doit(e[x].next[w],k,j+1);
30     }
31 }
32 int getit(int x,int k,int j){
33     if(k<j){
34         return e[x].count;
35     }
36     int w=a[j]-'a';
37     if(!e[x].next[w]){
38         return 0;
39     }
40     else{
41         return getit(e[x].next[w],k,j+1);
42     }
43 }
44 int main(){
45     memset(e,0,sizeof(e));
46     a[0]=getchar();
47     while(1){
48         int i=1;a[i]=getchar();
49         while(a[i]!='\n'){
50             i++;
51             a[i]=getchar();
52         }
53         doit(0,i-1,0);
54         a[0]=getchar();
55         if(a[0]=='\n'){
56             break;
57         }
58     }
59     while(~scanf("%s",&a)){
60         int z=strlen(a)-1;
61         printf("%d\n",getit(0,z,0));
62     }
63     return 0;
64 }
View Code

 

转载于:https://www.cnblogs.com/137shoebills/p/7786487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值