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 }
时间: 2024-08-05 01:54:28

poj 2418 bst统计字符串的相关文章

POJ - 2418 代码

使用Trie树完成.比STL map 快很多. 输出时DFS,用一个字符数组记录当前字符串. 走到是字符串的结点就输出. 代码如下. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int MaxL =

POJ 2418 Hardwood Species(字典树)

题目链接:POJ 2418 Hardwood Species [题意]给出一大串树的名字,可能有重复,然后按字典序输出名字和百分比. [思路]我已开始偷懒用了map来做,这道题给的时间是10s,用map的8s也还是水过了,真是神奇啊,后来还是写了一下字典树,700ms+就过了,效率提升显著啊.这里要注意的是建字典树的ChildSize是256,题目输入不只有字母,还有一些其它字符. 下面贴上代码,先是map的: 1 /* 2 ** POJ 2418 Hardwood Species 3 ** C

POJ 2418 Hardwood Species(STL中map的应用)

题目地址:POJ 2418 通过这个题查了大量资料..知道了很多以前不知道的东西.... 在代码中注释说明吧. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue&

POJ 2418 Hardwood Species Trie解法

计算一个字符串数组中有多少个重复字符串出现. 如果直接使用map容器,那么这条题就很简单了,一下就AC了,因为map已经处理好一切了: 不过时间超过1532ms,有点慢. 如下: int main() { map<string, int> msi; int total = 0; char treeName[40]; while (gets(treeName)) { msi[treeName]++; total++; } for (map<string, int>::iterator

poj 2418 Hardwood Species (trie 树)

链接:poj 2418 题意:给定一些树的种类名,求每种树所占的百分比,并按字典序输出 分析:实质就是统计每种树的数量n,和所有树的数量m, 百分比就为 n*100./m 由于数据达到一百万,直接用数组查找肯定超时, 可以用trie树,空间换取时间 注:这题树的品种名除了包括大写字母,小写字母和空格外,还有其他字符, 所以要注意trie树的子结点的个数 #include<cstdio> #include<cstdlib> #include<cstring> #inclu

二叉排序树统计字符串中出现的字符及其次数

二叉排序树统计字符串 结点的类型: typedef struct tnode { char ch; //字符 int count; //出现次数 struct tnode *lchild,*rchild; } tnode,*BTree; 完整代码 //文件名:exp9-5.cpp #include<iostream> using namespace std; #define MAXWORD 100 typedef struct tnode // typedef tnode *BTree { c

统计字符串中单词的个数

1.单纯统计单词个数,单词与单词之间只考虑空格的情况 // word_statistic.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; #define M 10000 #define N 20 int _tmain(int argc, _TCHAR* argv[]) { char str1[M]={0};

POJ 2309 BST

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8565   Accepted: 5202 Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we c

POJ 2309 BST 树状数组基本操作

Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until t