POJ2418 二叉排序树 && 字典树写法
来源:互联网 发布:怎么用cdr做淘宝店招 编辑:程序博客网 时间:2024/05/18 08:00
字典树
#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{int count;struct node *next[100];node(){count=0;memset(next,0,sizeof(next));}};node *h,*p,*q; int ans;char sx[40];void insert(char *s){int i,j,k;p=h;k=strlen(s);for(i=0;i<k;i++){if(p->next[s[i]-' ']==NULL){q=new node;p->next[s[i]-' ']=q;}p=p->next[s[i]-' '];}if(p->count==0)p->count=1;else p->count++;}void out(node *t,int x){if(t==0)return;for(int i=0;i<=95;i++)if(t->next[i]!=0){sx[x]=i+' ';if(t->next[i]->count!=0){sx[x+1]='\0';printf("%s %.4f\n",sx,100.0*t->next[i]->count/ans);}out(t->next[i],x+1);}return;}int main(){char s[40];h=new node;while(gets(s)!=NULL){//if(strcmp(s,"0")==0)break;insert(s);ans++;};out(h,0);return 0;}
二叉排序树
#include<cstdio>#include<algorithm>#include<cstring>#include<string>using namespace std;struct node{char ch[100];int l,r;int cnt;}a[10001];int len,sum=0;void insert(int h,char *s){int cmp=strcmp(a[h].ch,s);if (cmp==0){a[h].cnt++;return ;}if(cmp<0){if(a[h].r==0){strcpy(a[++len].ch,s);a[len].cnt=1;a[h].r=len;}else insert(a[h].r,s);}else{if(a[h].l==0){strcpy(a[++len].ch,s);a[len].cnt=1;a[h].l=len;}else insert(a[h].l,s);}}void out(int h){if(a[h].l!=0)out(a[h].l);printf("%s %.4f\n",a[h].ch,100.0*a[h].cnt/sum);if(a[h].r!=0)out(a[h].r);}int main(){int i,j,k,m,n; char s[100]; gets(a[1].ch);a[1].cnt=1;len=1;sum=1; while(gets(s)!=NULL){ insert(1,s);sum++;}out(1);return 0;}
0 0
- POJ2418 二叉排序树 && 字典树写法
- POJ2418 二叉排序树 && 字典树写法
- poj2418 二叉排序树
- poj2418~Hardwood Species~二叉排序树
- POJ2418 Hardwood Species(字典树)
- poj2418 字典树裸题
- POJ2418 Hardwood Species(二叉搜索树+字典树+MAP)
- poj2418二叉查找树
- POJ2418 二叉查找树
- POJ2418
- poj2418
- poj2418
- poj2418
- poj2418
- poj2418
- 二叉排序树 字典树 树中统计
- 二叉排序树:POJ2418-Hardwood Species(外加字符串处理)
- 字典树数组形式写法
- Recaman's Sequence
- 【算法题】Leedcode 335. Self Crossing 自交问题
- socket(TCP)
- 深度学习笔记一
- 有参考或学习价值的项目
- POJ2418 二叉排序树 && 字典树写法
- postgresql 常用命令
- 第十二周项目一 阅读程序,请写出这些程序的运行结果(3)
- 知识点——多态性、运算符重载、双目运算符重载实现、单目运算符重载实现
- Handler
- sql server 查询DB replication 信息
- 第五次作业
- 构造函数,数组取最大值,最小值的体现。
- nginx location配置详细解释