POJ_2418_Hardwood Species(map实现、Trie tree实现)
来源:互联网 发布:cmseasy 知乎 编辑:程序博客网 时间:2024/06/09 21:46
题型:综合题,各种方法解决。
分析:
感觉这是一个强大的题目,可以用各种高难度数据结构解决。
思想很简单,就不多说了。
代码:
map实现:
#include <iostream>#include <string>#include <map>#include <iterator>#include <cstdio>using namespace std;int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); string s; int cnt = 0; map<string,int> tree; while(getline(cin,s)!=NULL){ tree[s]++;cnt++; } map<string,int>::iterator iter; for(iter = tree.begin();iter != tree.end();iter++){cout<<iter->first; printf(" %.4f\n",iter->second*100.0/cnt); } return 0;}
Trie tree实现:
#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#define MAXN 130using namespace std;struct Node{char name[35];int num;bool tree;Node *next[MAXN];Node(){num = 0;tree = false;for(int i=0;i<MAXN;i++){next[i] = NULL;}}};int sum;void insert(Node *root,char *str){int len = strlen(str);Node *q;q = root;for(int i=0;i<len;i++){int x = str[i];if(q->next[x] == NULL){q->next[x] = new Node;}q = q->next[x];}q->num++;q->tree = true;strcpy(q->name,str);}void output(Node *root){Node *q;q = root;if(q->tree){printf("%s %.4f\n",q->name,q->num*100.00/sum);}for(int i=0;i<MAXN;i++){if(q->next[i] != NULL){//q = q->next[i];output(q->next[i]);}}}int main(){//freopen("in.txt","w",stdin);//freopen("out.txt","r",stdout);Node *root = new Node;sum = 0;char str[35];while(gets(str) != 0){insert(root,str);//cout<<"****"<<str<<endl;sum++;}output(root);return 0;}
0 0
- POJ_2418_Hardwood Species(map实现、Trie tree实现)
- POJ 2418 Hardwood Species(STL map or Trie tree)
- Species Tree(HashTable实现)
- Trie-Tree 实现
- Trie Tree简单实现
- POJ 2418 Hardwood Species (trie树\map)
- Trie (prefix tree) 实现 (Java)
- Trie Tree的c++实现
- Trie (prefix tree) 实现 (Java)
- Trie (prefix tree) 实现 (C++)
- Trie Tree匹配算法实现
- POJ 2418 Hardwood Species(trie 树 MAP qsort)
- [ACM] POJ 2418 Hardwood Species (Trie树或者map)
- [ACM] POJ 2418 Hardwood Species (Trie树或者map)
- POJ-2418 Hardwood Species(Trie树)(map)
- 使用Trie树实现的MAP
- Python学习笔记:Trie Tree的实现
- 使用 trie tree 实现 auto-completion
- java如何打JAR包(转)
- Android控件系列之ProgressBar&在Android中利用Handler处理多线程(★推荐)
- 为什么会存在using filesort
- C++模板机制笔记
- auto_ptr解析
- POJ_2418_Hardwood Species(map实现、Trie tree实现)
- QT中QWidget、QDialog及QMainWindow的区别
- delphi中获得当前时间
- android NDK jni下的c文件 Unresolved inclusion
- java代理模式 (转)
- java中的输入输出(转载)
- C# checked与unchecked用法
- 回溯与递归
- C# TextBox中的Validating与Validated事件