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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值