poj 2418 Hardwood Species
来源:互联网 发布:淘宝假食品流通许可证 编辑:程序博客网 时间:2024/04/25 09:50
统计每个单词出现的频率。这题可以用各种数据结构做,作为练习,又敲了一个AVL树。
#include <iostream> #include <stdio.h> #include <string> #include <map> #include <vector> #include <stack> #include <queue> #include <string.h> #include <algorithm> #include <math.h> using namespace std; char str[32];struct avl{char data[32];int height;avl* pleft;avl* pright;int size;int cnt;};int Height(avl* pNode){if(NULL==pNode)return -1;return pNode->height;}avl* LLRotate(avl* pNode){avl* l=pNode->pleft;pNode->pleft=l->pright;l->pright=pNode;pNode->height=max(Height(pNode->pleft),Height(pNode->pright))+1;l->height=max(Height(l->pleft),Height(l->pright))+1;return l;}avl* RRRotate(avl* pNode){avl* r=pNode->pright;pNode->pright=r->pleft;r->pleft=pNode;pNode->height=max(Height(pNode->pleft),Height(pNode->pright))+1;r->height=max(Height(r->pleft),Height(r->pright))+1;return r;}avl* LRRotate(avl* pNode){pNode->pleft=RRRotate(pNode->pleft);return LLRotate(pNode);}avl* RLRotate(avl* pNode){pNode->pright=LLRotate(pNode->pright);return RRRotate(pNode);}avl* insert(avl* pNode,char* data){if(pNode==NULL){pNode=new avl();memcpy(pNode->data,data,sizeof(str));pNode->height=0;pNode->size=1;pNode->cnt=1;pNode->height=max(Height(pNode->pleft),Height(pNode->pright));return pNode;}int cmp=strcmp(data,pNode->data);if(cmp<0){pNode->pleft=insert(pNode->pleft,data);pNode->size++;if(Height(pNode->pleft)-Height(pNode->pright)==2){//不平衡 if(data<pNode->pleft->data){pNode=LLRotate(pNode);}else{pNode=LRRotate(pNode);}}}else if(cmp>0){pNode->pright=insert(pNode->pright,data);pNode->size++;if(Height(pNode->pright)-Height(pNode->pleft)==2){//不平衡 if(data>pNode->pright->data){pNode=RRRotate(pNode);}else{pNode=RLRotate(pNode);}}}else{pNode->cnt++;}pNode->height=max(Height(pNode->pleft),Height(pNode->pright));return pNode;}int tot;void print(avl* pNode){if(pNode->pleft){print(pNode->pleft);}printf("%s %.4lf\n",pNode->data,pNode->cnt*100.0/tot );if(pNode->pright){print(pNode->pright);}}int main(){tot=0;avl* root=NULL;while(gets(str)){root=insert(root,str);tot++;}print(root);return 0;}
0 0
- poj 2418 Hardwood Species
- POJ 2418 Hardwood Species
- poj 2418Hardwood Species
- POJ 2418 Hardwood Species
- poj 2418 Hardwood Species
- POJ 2418 Hardwood Species
- POJ 2418 Hardwood Species
- poj 2418Hardwood Species
- POJ 2418 Hardwood Species
- POJ 2418 Hardwood Species
- POJ 2418 Hardwood Species
- POJ 2418 Hardwood Species
- POJ 2418 Hardwood Species
- poj 2418 Hardwood Species
- poj 2418 Hardwood Species
- poj 2418 Hardwood Species
- poj 2418 Hardwood Species
- POJ 2418 Hardwood Species
- JQuery+ajax+jsonp 跨域访问
- ContentResolver.query方法的使用
- STL学习小结
- oc中类的继承
- IOS多线程之GCD(2)
- poj 2418 Hardwood Species
- DEVICE_ATTR的使用
- UVa 128 Software CRC(模运算)
- 备库log_archive_dest_2的问题
- android 手机上设置呼叫转移
- Java基础课
- 详解JAVA高质量代码之数组与集合
- 互联网时代下的产品思维
- extjs日常笔记