Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

会错了树的意思,结果成了这样,用二叉时间大概是多少,我的700+ms

Posted by lizeliang at 2009-06-07 17:52:39 on Problem 2418
#include<stdio.h>
#define KIDS 128
#define NAMELONG 100

typedef struct node
{
    char abc;
    int num;
    struct node * pnode[KIDS];
}node;

void inwoods(char s[NAMELONG],node * root);
void outwoods(char pri[NAMELONG],int height,node * root,const sum);
void destroyWoods(node * root);

int main()
{
    int i,sum=0;
    char s[NAMELONG];
    node * root;
    root=(node *)malloc(sizeof(node));
    root->abc='a';
    root->num=0;
    for(i=0;i<KIDS;i++)
        root->pnode[i]=NULL;
    while(gets(s))
    {
        inwoods(s,root);
        sum++;
    }
    outwoods(s,0,root,sum);
    destroyWoods(root);
    return 0;
}

void inwoods(char s[NAMELONG],node * root)
{
    int i,j;
    node * pmove=root;
    for(i=0;s[i]!='\0';i++)
    {
        if(pmove->pnode[s[i]]==NULL)
        {
            pmove->pnode[s[i]]=(node *)malloc(sizeof(node));
            pmove=pmove->pnode[s[i]];
            for(j=0;j<KIDS;j++)
                pmove->pnode[j]=NULL;
            pmove->abc=s[i];
            pmove->num=0;
        }
        else
            pmove=pmove->pnode[s[i]];
    }
    pmove->num=pmove->num+1;
}
void outwoods(char pri[NAMELONG],int height,node * root,const sum)
{
    int i,j=0;
    if(root!=NULL)
    {
        if(height!=0)
        {
            pri[height-1]=root->abc;
            if(root->num!=0)
            {
                pri[height]='\0';
                while(pri[j]!='\0')
                {
                    printf("%c",pri[j]);
                    j++;
                }
                printf(" %.4f\n",((double)root->num/sum)*100);
            }
        }
        for(i=0;i<KIDS;i++)
            outwoods(pri,height+1,root->pnode[i],sum);  
    }
}
void destroyWoods(node * root)
{
    int i;
    if(root!=NULL)
    {
        for(i=0;i<KIDS;i++)
            destroyWoods(root->pnode[i]);
        free(root);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator