HDU1075 What Are You Talking About 解题报告--字典树

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 10326    Accepted Submission(s): 3289


Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 

Output
In this problem, you have to output the translation of the history book.
 

Sample Input
 
   
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output
 
   
hello, i'm from mars. i like earth!
Hint
Huge input, scanf is recommended.
 

Author
Ignatius.L
#include
using namespace std;
struct node
{
	char str[11];//火星文译文
	node *next[26];//字典树
	node()
	{
		memset(str,0,sizeof(str));
		memset(next,0,sizeof(next));
	}
};
node *root=NULL;
void build(char *a,char *b) 
{
	node *p=root; 
	int len=strlen(b);
	int i;
	for(i=0;inext[b[i]-'a']==NULL)
		{
			p->next[b[i]-'a']=new node; 
		}
		p=p->next[b[i]-'a'];
	}//建树,开辟空间
	strcpy(p->str,a);//最后一个节点上存进火星文译文
}
void find(char *s) 
{
	node *p=root;
	int len=strlen(s);
	int i;
	int f=0;
	for(i=0;inext[s[i]-'a']==NULL)//如果不匹配,输出原文结束
		{
			printf("%s",s);
			return ;
		}
		p=p->next[s[i]-'a']; //这个字母匹配移向下一个
	}
	if(strcmp(p->str,"")==0)//如果只有前部分匹配,没匹配到最后则不存在译文输出原文
		printf("%s",s);
	else
		printf("%s",p->str);//如果都过了输出译文
}
int main()
{
	root=new node;
	char str[1001],a[11],b[11],cc[1001];
	int i;
	scanf("%s",str);//输入STAR
	while(scanf("%s",a))
	{
		if(strcmp(a,"END")==0) break;
		scanf("%s",b);	
		build(a,b); //建树
	}
	scanf("%s%*c",str);//输入STAR接收个回车
	while(gets(str))
	{
		if(strcmp(str,"END")==0) break;
		int ll=strlen(str);
		int u=0;
		for(i=0;i'z'||i==ll-1)//如果接到不是字母或则到了最后一个
			{
				if(str[i]>='a'&&str[i]<='z')//如果最后一个还是字母放入字符串里
					cc[u++]=str[i];
				cc[u]='\0';
				find(cc);//检索
				u=0;
				if(str[i]<'a'||str[i]>'z')//如果是符号输出
					printf("%c",str[i]);
			}
			else
				cc[u++]=str[i];//将字符一个个接收
		}
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(树,HDU,字典树)