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

964MS,求优化

Posted by speedcell4 at 2011-04-21 16:12:27 on Problem 2503
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
    char *a;
    struct node * next[26];
};
typedef struct node * tree;
typedef struct node * position;

tree creat(void)
{
    tree now=NULL;
    return now;
}
position push(char a[],char b[],tree n,int l,int i)
{
    if(n==NULL)
    {
        n=(tree)malloc(sizeof(struct node));
        for(int i=0;26-i;i++) n->next[i]=NULL;
        n->a=NULL;
    }
    if(l==i)
    {
        n->a=(char *)malloc(sizeof(char)*12);
        strcpy(n->a,b);
    }
    else
    {
        int now=a[i]-'a';
        n->next[now]=push(a,b,n->next[now],l,i+1);
    }
    return n;
}
char *find(char a[],tree n,int l,int i)
{
    if(n==NULL) return NULL;
    if(l==i) return n->a;
    else
    {
        int now=a[i]-'a';
        return find(a,n->next[now],l,i+1);
    }
}
int main()
{
    tree now=creat();
    char a[30],b[30];
    while(scanf("%c",&a[0]),a[0]!='\n')
    {
        int i;
        for(i=1;;i++)
        {
            scanf("%c",&a[i]);
            if(a[i]==' ') break;
        }
        a[i]='\0';
        scanf("%s",b);
        getchar();
        int l=strlen(b);
        now=push(b,a,now,l,0);
    }
    while(scanf("%s",a)!=EOF)
    {
        int l=strlen(a);
        char *here=find(a,now,l,0);
        if(here==NULL) cout<<"eh"<<endl;
        else cout<<here<<endl;
    }
}

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