POJ 2418 Hardwood Species

  字典树 或者  map......

  map  8000+ ms

  

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <cstdlib>
 7 #include <queue>
 8 #include <map>
 9 #include <string>
10 #include <utility>
11 #include <vector>
12 #include <iomanip>
13 
14 using namespace std;
15 
16 struct cmp
17 {
18     bool operator() (pair<string,int> a, pair<string,int> b)
19     {
20         return a > b;
21     }
22 };
23 
24 priority_queue<int,vector< pair<string,int> >,cmp> q;
25 
26 map<string,int> tree;
27 
28 int main()
29 {
30 
31 
32     pair<string,int> stu;
33 
34     string name;
35 
36     int count = 0;
37 
38     while(getline(cin,name) != NULL)
39     {
40         ++tree[name];
41         ++count;
42     }
43 
44     map<string,int>::iterator it = tree.begin();
45 
46     while(it != tree.end())
47     {
48         cout<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(4)<<(it->second*100.0/count)<<endl;
49         ++it;
50     }
51 
52     return 0;
53 }
View Code

  

  字典树 1100+ ms 

  

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 int max;
 6 
 7 struct T
 8 {
 9     char name[40];
10     int sum,mark;
11     struct T *r,*l;
12 };
13 
14 void init(struct T *root)
15 {
16     root->r = NULL;
17     root->l = NULL;
18     root->sum = 0;
19     root->mark = 1;
20 }
21 
22 void link(struct T *p,char *temp)
23 {
24         if(p->mark)
25         {
26             strcpy(p->name,temp);
27             p->sum++;
28             p->mark = 0;
29         }
30         else
31         {
32             int    mark = strcmp(temp,p->name);
33             if(!mark) p->sum++;
34             else if(mark > 0)
35             {
36                 if(p->l != NULL)
37                 {
38                     link(p->l,temp);
39                     return;
40                 }
41                 else
42                 {
43                     struct T *t;
44                     t = (struct T *)malloc(sizeof(struct T));
45                     init(t);
46                     p->l = t;
47                     link(p->l,temp);
48                     return;
49                 }
50             }
51             else if(mark < 0)
52             {
53                 if(p->r != NULL)
54                 {
55                     link(p->r,temp);
56                     return;
57                 }
58                 else
59                 {
60                     struct T *t;
61                     t = (struct T *)malloc(sizeof(struct T));
62                     init(t);
63                     p->r = t;
64                     link(p->r,temp);
65                     return;
66                 }
67             }
68         }
69 }
70 
71 void output(struct T *root)
72 {
73     if(root->r != NULL)
74     {
75         output(root->r);
76         printf("%s %.4lf
",root->name,100.0*root->sum/max);
77     }
78     else printf("%s %.4lf
",root->name,100.0*root->sum/max);
79     if(root->l != NULL)
80     {
81         output(root->l);
82     }
83 }
84 
85 int main()
86 {
87     struct T *root;
88     root = (struct T *)malloc(sizeof(struct T));
89     init(root);
90     char temp[40];
91     max = 0;
92     while(gets(temp) != NULL)
93     {
94         max++;
95         link(root,temp);
96     }
97     output(root);
98     return 0 ;
99 }
View Code
原文地址:https://www.cnblogs.com/zmx354/p/3268906.html