回复 2楼 sunyh1999
											看了那有注释的代码,貌似有点懂了,于是就去做这题http://acm.hdu.;
Sample和自己想的几个案例饿都过了,可是提交时说超内存了

帮帮看看哪错了 还是哪可以优化下啊
我的代码是这样的希望能看懂啊

#include<stdio.h>
#include<stdlib.h>
struct tree
{
    int flag;
    struct tree *next[10];
};
void tree1(struct tree *root)
{
    int i;
    for(i=0;i<10;i++)
    {
        root->flag=0;
        root->next[i]=NULL;
    }
}
int count;
void tire_tree(struct tree *root,char word[10])
{
    struct tree *p=root;
    int tem,i=0;
    while(word[i])
    {
        if(p->flag==1)//判断它的前缀单词是否存在
           count++;
        tem=word[i]-'0';
        if(p->next[tem]==NULL)
        {
            p->next[tem]=(struct tree *)malloc(sizeof(struct tree));
            tree1(p->next[tem]);
        }
        if(p->next[tem]&&word[i+1]=='\0')//判断以它为前缀的单词是否存在
        count++;
        p=p->next[tem];
        i++;
    }
    p->flag=1;
}
int main()
{
    int t,n,i;
    char word[10];
    struct tree *root=NULL;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            getchar();
            count=1;
            root=(struct tree *)malloc(sizeof(struct tree));
            tree1(root);
            for(i=0;i<n;i++)
            {
                gets(word);
                tire_tree(root,word);
            }
            if(count>1)
                printf("No\n");
            else
                printf("Yes\n");
        }
    }
    return 0;
}
[
 本帖最后由 草狼 于 2010-8-7 10:56 编辑 ]