现在的位置: 首页 > 综合 > 正文

hdu 1075 字典树

2013年08月29日 ⁄ 综合 ⁄ 共 1245字 ⁄ 字号 评论关闭

int 定义为char,re了2个小时,人才啊。。。。。。。。。。。。

注意点,abc 对应efg,输入efg应该输出abc,输入ef呢?

注意输入前缀无输出的情况

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<cstring>
#include"ctype.h"  
struct Dictree
{
	int no;
	struct Dictree *next[26];
}*root;
char str[500005][15],s[3005],p[2005];
void create_tree();
void insert(char *,int );
int find(char *);
int main()
{
	int i=0,j=0,k=0,temp,len;
	create_tree();
	scanf("%s",s);
	while(scanf("%s",str[i]),strcmp(str[i],"END")!=0)
	{
		scanf("%s",p); 
		insert(p,i++);
	}
	scanf("%s",s),getchar();
	while(gets(s))
	{
		if(strcmp(s,"END")==0)
			break;
		len=strlen(s);
		k=0;
		for(j=0;j<len;j++)
		{
			if(islower(s[j])) p[k++]=s[j];
			else
			{
				if(k)
				{
					p[k]=0;
					temp=find(p);
					if(temp==-1) printf("%s",p);
					else 	printf("%s",str[temp]);
				}
				if(s[j]) printf("%c",s[j]);
				k=0;
			}
		}
		memset(s,0,sizeof(s));
		putchar('\n'); 
	}
	return 0;
}
void insert(char *s,int k)
{
	int i,j;
	struct Dictree *p,*t;
	p=root;
	for(i=0;s[i];i++)
	{
		if(p->next[s[i]-'a']==NULL)
		{
			t=(struct Dictree*)malloc(sizeof(struct Dictree));
			t->no=-1;
			for(j=0;j<26;j++)
				t->next[j]=NULL;
			p->next[s[i]-'a']=t;
		}
		p=p->next[s[i]-'a'];
	}
	p->no=k;
}
int find(char *s)
{
	int i;
	struct Dictree *p=root;
	for(i=0;s[i];i++)
	{
		if(p->next[s[i]-'a']==NULL)
			return -1;
		else p=p->next[s[i]-'a'];
	}
	return p->no;
}
void create_tree()
{
	int i;
	root=(struct Dictree*)malloc(sizeof(struct Dictree));
	root->no=-1;
	for(i=0;i<26;i++) root->next[i]=NULL;
}

抱歉!评论已关闭.