POJ - 2418 Hardwood Species(map,trie,BST)
2015-11-06 12:43
295 查看
1、输入若干行树名,输入结束后,按字典序输出树名及其所占百分比。
2、多种方法:map,trie,BST
3、
map:
View Code
BST:
2、多种方法:map,trie,BST
3、
map:
#include<iostream> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; const int MAX=128; char tree[10005][35];//存储不同的树名 int species=0;//树的种类数目 int totalNum=0;//树的总数目 struct Trie { Trie *next[MAX]; int num;//出现次数 }; Trie *root=new Trie; void createTrie(char *str) { ++totalNum; int len = strlen(str); Trie *p = root, *q; for(int i=0; i<len; ++i) { int id = str[i]; if(p->next[id] == NULL) { q = (Trie *)malloc(sizeof(Trie)); //q = new Trie; for(int j=0; j<MAX; ++j){ q->next[j] = NULL; q->num=0; } p->next[id] = q; } p = p->next[id]; } if(p->num==0)strcpy(tree[species++],str); ++p->num; } int findTrie(char *str) { int len = strlen(str); Trie *p = root; for(int i=0; i<len; ++i) { int id = str[i]; p = p->next[id]; if(p == NULL) //若为空集,表示不存以此为前缀的串 return 0; } return p->num; } int cmp(const void *a,const void *b){ return strcmp((char *)a,(char *)b);//升序 } int main() { char str[35]; int i; for(i=0; i<MAX; i++){ root->next[i]=NULL; root->num=0; } while(gets(str)){ createTrie(str); } qsort(tree,species,sizeof(tree[0]),cmp); for(i=0;i<species;++i) printf("%s %.4f\n",tree[i],double(findTrie(tree[i]))/double(totalNum)*100); return 0; }
View Code
BST:
相关文章推荐
- 记我升级电脑硬盘,硬盘升级系统迁移免除安装系统的烦恼 (硬盘克隆)
- Android 线程池模拟多线程并发下载任务
- 程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
- 升级xcode7 遇到的问题 Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:]
- mysql主从同步,双向同步
- 不可变类
- Barcode Professional for ASP.NET使用教程:HTML页面显示条码
- SAX与DOM
- linux与 windows环境的Subversion版本控制企业架构搭建之svn+ssh
- Git:廖雪峰git教程
- 以后就专注电子笔记了
- 预处理
- java GUI简单记事本
- Log4net用法(App.config配置)
- 数据结构与算法javascript描述(二) 优先队列实例
- 深度相机的人物检测、分割、跟踪算法,openNI的NITE采用的算法,kinect,华硕xtion
- jQuery:插件开发步骤
- Barcode Professional for ASP.NET使用教程:HTML页面显示条码
- 进程和线程
- 要想清楚剪切板内容