poj 2418 bst统计字符串

数据比较随机,直接bst可以过。

 1 #include <cstring>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 const int N = 101;
 6 int tot;
 7 
 8 struct Node 
 9 {
10     Node * ch[2];
11     char v[N];    
12     int cnt;
13     int cmp( char * x )
14     {
15         int tmp = strcmp( x, v );
16         if ( tmp == 0 ) return -1;
17         return tmp < 0 ? 0 : 1;
18     }
19 };
20 
21 void insert( Node * & o, char * x )
22 {
23     if ( o == NULL )
24     {
25         o = new Node ();
26         o->ch[0] = o->ch[1] = NULL;
27         o->cnt = 1;
28         strcpy( o->v, x );
29         return ;
30     }
31     int d = o->cmp(x);
32     if ( d == -1 ) 
33     {
34         o->cnt++;
35         return ;
36     }
37     insert( o->ch[d], x );
38 }
39 
40 void free( Node * o )
41 {
42     if ( o != NULL )
43     {
44         free(o->ch[0]);
45         free(o->ch[1]);
46         delete o;
47     }
48 }
49 
50 void inorder( Node * o )
51 {
52     if ( o )
53     {
54         inorder(o->ch[0]);
55         printf("%s %.4f\n", o->v, 100.0 * o->cnt / tot);
56         inorder(o->ch[1]);
57     }
58 }
59 
60 Node * root;
61 char str[N];
62 
63 int main ()
64 {
65     root = NULL;
66     tot = 0;
67     while ( gets(str) != NULL )
68     {
69         tot++;
70         insert( root, str );
71     }
72     inorder(root);
73     free(root);
74     return 0;
75 }

 

转载于:https://www.cnblogs.com/huoxiayu/p/4466408.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值