http://162.105.81.212/JudgeOnline/problem?id=2418
刚开始用map水过, 1407ms
然后就去Discuss里瞧了瞧,
很多人说用二叉排序树做,
于是我就又去种了颗二叉排序树, 结果2829ms, 囧囧
当是熟练下如何种树吧.
//二叉排序树 #include<iostream> using namespace std; struct Tree { char name[31]; int time, left, right; }tmp, tree[1000001]; int len, tot; void insert(Tree* Root, Tree t) { if(len == 0) { strcpy(Root->name, t.name); Root->left = -1; Root->right = -1; Root->time = 1; len++; return; } if(!strcmp(t.name, Root->name)) { Root->time++; return; } else if(strcmp(t.name, Root->name) > 0) { if(Root->right != -1) { Root = &tree[Root->right]; insert(Root, t); } else { Root->right = len; strcpy(tree[len].name, t.name); tree[len].time = 1; tree[len].left = -1; tree[len].right = -1; len++; return; } } else if(strcmp(t.name, Root->name) < 0) { if(Root->left != -1) { Root = &tree[Root->left]; insert(Root, t); } else { Root->left = len; strcpy(tree[len].name, t.name); tree[len].time = 1; tree[len].left = -1; tree[len].right = -1; len++; return; } } } void printTree(Tree* Root) { //if(Root->time != -1) if(Root->time != 0) { if(Root->left > 0) printTree(&tree[Root->left]); printf("%s %.4lf/n", Root->name, 100.0*Root->time/tot); if(Root->right > 0) printTree(&tree[Root->right]); } } int main() { //for(int i=0; i<1000001; i++) // tree[i].time = -1; char c[31]; tot = len = 0; while(gets(c)) { strcpy(tmp.name, c); tot++; insert(&tree[0], tmp); } printTree(&tree[0]); return 0; } //map /* #include<iostream> #include<map> #include<string> using namespace std; int main() { int n = 0; string str; map<string,int> m; map<string,int>::iterator it, end; char c[31]; while(gets(c)) { str = c; m[str]++; n++; } it = m.begin(); end = m.end(); while(it != end) { const char* s = it->first.c_str(); printf("%s", s); printf(" %.4lf/n",100.000000*(it->second)/(double)n); it++; } return 0; } */
本文通过实例分析了使用二叉排序树与map解决特定问题的效果对比,从效率和实现角度进行了深入探讨,并提供了代码实现细节。
408

被折叠的 条评论
为什么被折叠?



