hdu 1247 (字典树)

点击打开链接


分析:

对每一个字符串进行分解,看它分解的两部分能否在字典树中find,用hash标记。。。

WA了好几遍。。。


下面是大神的ice_Crazy的代码。。

#include"stdio.h"
#include"stdlib.h"
#include"string.h"


struct dictree
{
    struct dictree *child[26];
    int flag;
};
struct dictree *root;


char str[50011][50];


void insert(char *source)
{
    int i,j;
    int len;
    struct dictree *current,*newnode;


    len=strlen(source);
    current=root;


    for(i=0;i<len;i++)
    {
        if(current->child[source[i]-'a']!=0)
            current=current->child[source[i]-'a'];
        else
        {
            newnode=(struct dictree *)malloc(sizeof(struct dictree));
            for(j=0;j<26;j++)    newnode->child[j]=0;
            newnode->flag=0;
            current->child[source[i]-'a']=newnode;
            current=newnode;
        }
    }


    current->flag=1;
}


int find(char *source)
{
    int i;
    int len;
    struct dictree *current;


    len=strlen(source);
    current=root;
    for(i=0;i<len;i++)
    {
        if(current->child[source[i]-'a']!=0)
            current=current->child[source[i]-'a'];
        else    return 0;
    }


    return current->flag;
}


int main()
{
    char t1[50],t2[50];
    int t;
    int k;
    int i,l,j,j2;
    int len;
    int hash[50011];


    root=(struct dictree *)malloc(sizeof(struct dictree));
    for(j=0;j<26;j++)    root->child[j]=0;
    root->flag=0;


    k=0;
    while(scanf("%s",str[k])!=-1)
    {
        insert(str[k]);
        k++;
    }


    for(i=0;i<k;i++)
    {
        len=strlen(str[i]);
        hash[i]=0;


        if(len==1)    continue;
        
        t=len-1;
        for(l=1;l<=t;l++)
        {
            for(j=0;j<l;j++)        t1[j]=str[i][j];
            t1[j]=0;
            for(j2=0;j<len;j++,j2++)t2[j2]=str[i][j];
            t2[j2]=0;

            if(find(t1)&&find(t2))    {hash[i]=1;break;}
        }
    }


    for(i=0;i<k;i++)    if(hash[i])    printf("%s\n",str[i]);


    return 0;
}


我的代码:(还没A呢)

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct tree
{
	struct tree *child[26];
	int num;
};
struct tree *root;
void insert(char *p)
{
	int i,j;
	int len;
	struct tree *cur,*nee;
	cur=root;
	len=strlen(p);
	for(i=0;i<len;i++)
	{
		if(cur->child[p[i]-'a']!=0)
		{
			cur=cur->child[p[i]-'a'];
			//cur->num++;
		}
		else
		{
			nee=(struct tree*)malloc(sizeof(struct tree));
			for(j=0;j<26;j++)
				nee->child[j]=0;
			nee->num=0;
			cur->child[p[i]-'a']=nee;
			cur=nee;
		}
	}
	cur->num=1;
}
int find(char *p)
{
	int i;
	int len;
	struct tree *cur;
	cur=root;
	len=strlen(p);
	for(i=0;i<len;i++)
	{
		if(cur->child[p[i]-'a']!=0)
			cur=cur->child[p[i]-'a'];
		else return 0;
	}
	return cur->num;
}
int hash[50011];
char str[50011][51];
int main()
{
	int i,j,l,k;
	int t1,t2,len;
	char s1[55],s2[55];
	
	root=(struct tree*)malloc(sizeof(struct tree));
	root->num=0;
	for(i=0;i<26;i++)
		root->child[i]=0;

	k=0;
	while(gets(str[k]))
		insert(str[k++]);

	for(i=0;i<k;i++)
	{
		hash[i]=0;
		len=strlen(str[i]);
		if(len==1)continue;
		for(j=1;j<len;j++)
		{
			t1=t2=0;
			for(l=0;l<j;l++)
				s1[t1++]=str[i][l];
			s1[t1]=0;
			for(;l<len;l++)
				s2[t2++]=str[i][l];
			s2[t2]=0;
			if(find(s1)&&find(s2)){hash[i]=1;break;}
		}
	}
	for(i=0;i<k;i++)
	{
		if(hash[i])printf("%s\n",str[i]);
	}
	return 0;
}




你可能感兴趣的:(hdu 1247 (字典树))