您的位置:首页 > 其它

POJ 2418 Hardwood Species

2013-08-19 21:06 369 查看
  字典树 或者 map......

  map 8000+ ms

  

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int max;

struct T
{
char name[40];
int sum,mark;
struct T *r,*l;
};

void init(struct T *root)
{
root->r = NULL;
root->l = NULL;
root->sum = 0;
root->mark = 1;
}

void link(struct T *p,char *temp)
{
if(p->mark)
{
strcpy(p->name,temp);
p->sum++;
p->mark = 0;
}
else
{
int    mark = strcmp(temp,p->name);
if(!mark) p->sum++;
else if(mark > 0)
{
if(p->l != NULL)
{
link(p->l,temp);
return;
}
else
{
struct T *t;
t = (struct T *)malloc(sizeof(struct T));
init(t);
p->l = t;
link(p->l,temp);
return;
}
}
else if(mark < 0)
{
if(p->r != NULL)
{
link(p->r,temp);
return;
}
else
{
struct T *t;
t = (struct T *)malloc(sizeof(struct T));
init(t);
p->r = t;
link(p->r,temp);
return;
}
}
}
}

void output(struct T *root)
{
if(root->r != NULL)
{
output(root->r);
printf("%s %.4lf\n",root->name,100.0*root->sum/max);
}
else printf("%s %.4lf\n",root->name,100.0*root->sum/max);
if(root->l != NULL)
{
output(root->l);
}
}

int main()
{
struct T *root;
root = (struct T *)malloc(sizeof(struct T));
init(root);
char temp[40];
max = 0;
while(gets(temp) != NULL)
{
max++;
link(root,temp);
}
output(root);
return 0 ;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: