It's unbelievable that I got TLE in this problem. I think I handle the multiinput data well and use qsort to reduce the running time.Could someone find out the bugs?
[c]
#include<stdio.h>
#include<string.h>
struct treedata
{
long n;
char *name;
};
int comp(const void *,const void *);
void main(void)
{
int x,count,n,y,max;
long total;
double t;
char s[31];
struct treedata tree[10000];
max=0;
for(x=0;x<10000;x++)
tree[x].name=NULL;
scanf("%d\n",&count);
for(x=0;x<count;x++)
{
if(x)
printf("\n");
n=total=0;
while(gets(s)!=NULL)
{
if(!strlen(s))
break;
total++;
for(y=0;y<n;y++)
if(strcmp(s,tree[y].name)==0)
{
tree[y].n++;
break;
}
if(y==n)
{
if(!tree[n].name)
tree[n].name=(char *)malloc(sizeof(char)*31);
strcpy(tree[n].name,s);
tree[n].n=1;
n++;
}
}
qsort(tree,n,sizeof(struct treedata),comp);
for(y=0;y<n;y++)
{
t=tree[y].n;
printf("%s %.4lf\n",tree[y].name,t/(double)total*100);
}
if(n>max)
max=n;
}
for(x=0;x<max;x++)
free(tree[x].name);
}
int comp(const void *a,const void *b)
{
return strcmp(((const struct treedata *)a)->name,((const struct treedata *)b)->name);
}
[/c]
