hdu1075

/*
分析:
    感谢Lelouch的帮忙O(∩_∩)O~,标记部分是他的代码,

大神写的就是不一样~,比我的简单多的多了。


                                                                 2012-07-05    

*/








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


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


void insert(char *source,char *temp)
{
	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;
	strcpy(current->aim,temp);
}


int flag;
char ans[15];
void find(char *source)
{
	struct dictree *current;
	int i;
	int len;


	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	{flag=1;return ;}
	}
	if(current->flag==0)	{flag=1;return;}


	strcpy(ans,current->aim);
}


int main()
{
	char str1[15],str2[15];
	char str[3033];
	int i,j;


	int len;
	char temp[15];

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

	scanf("%s",str1);
	while(scanf("%s",str1),strcmp(str1,"END")!=0)
	{
		scanf("%s",str2);
		insert(str2,str1);
	}

	scanf("%s",str);
	getchar();
	while(gets(str),strcmp(str,"END")!=0)
	{
		/*****/
		len=strlen(str);
		j=0;
		for(i=0;i<len;i++)
		{
			if(islower(str[i]))	temp[j++]=str[i];
			else
			{
				if(j)
				{
					temp[j]=0;
					flag=0;
					find(temp);
					if(flag)	printf("%s",temp);
					else		printf("%s",ans);
				}
				if(str[i])		printf("%c",str[i]);
				j=0;
			}
		}
		/*****/
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(hdu1075)