hdu 1075 Trie树

What Are You Talking About

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


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!
题目大意题目的意思就是说输入以STAR字符串开始,接着是任意的几行,每行两个字符串,第二个字符串是火星文,第一个是其对应的英文单词,也就是火星文的单词翻译表,当输入END是表示翻译表输入结束。接着又是一个START表示文章的输入开始,即一个长的火星文字符串,包括标点符号,在翻译表中查找该文章中的火星文,如果存在就输出对应的英文单词,如果不存在就原样输出该火星文,测试数据只有一组,最后仍然是以END结束。
解题思路:同样是字典树的一种,但需注意一点,在创建字典树的时候,当一个火星文的字符串创建完毕后,在最后一个字符的结点中存入此火星文对应的英文单词,便于查找成功后的替换。其实这个题的思路同其它字典树一样,只不过在字符串的处理方面比较难,字典树的创建和查找很快就写出来了,但main函数中的字符串处理可真是把我难得不轻,用了我一下午的时间调试字符串这个问题,我也是快醉了。主要是当时没想到,后来想通了,发现也并没有那么难。后来提交通过后,真是表示呵呵了.......
具体思路在代码中有标注

具体代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
char s1[15];
char s2[15];
char s[3005];
struct Node
{
	char loc[15];//存放火星文对应的单词 
	Node *next[26];//26个字符的指针 
};
Node *root,*p,*q;
void Init(Node *root)//结点初始化 
{
	root->loc[0]='#';
	for(int i=0;i<26;i++)
		root->next[i]=NULL;
}
void Insert(char *s,char *res)//创建字典树 
{
	int i,v;
	for(i=0,p=root;i<strlen(s);i++)
	{
		v=s[i]-'a';
		if(p->next[v]==NULL)
		{
			q=(struct Node *)malloc(sizeof(Node));
			Init(q);
			p->next[v]=q;
		}
		p=p->next[v];
	}
	strcpy(p->loc,res);
}
Node *Search(char *s)//在字典树中查单词 
{
	int i,v;
	for(i=0,p=root;i<strlen(s);i++)
	{
		v=s[i]-'a';
		if(p->next[v]==NULL)
			break;
		p=p->next[v];
	}
	if(i==strlen(s))
		return p;
	else
		return NULL;
}
int main()
{
	root=(struct Node *)malloc(sizeof(Node));
	Init(root); p=root;
	gets(s1);//读取START字符串,表示开始 
	while(scanf("%s",s1))
	{
		if(strcmp(s1,"END")==0)
			break;
		scanf("%s",s2);
		Insert(s2,s1);
	}
	getchar();
	gets(s1);//读取START字符串,表示开始 
	while(gets(s))//需要翻译的火星文
	{
		if(strcmp(s,"END")==0)
			break;
		int i,j;
		int len=strlen(s);
		s[len]='\n';//每个字符串最后一位加换行符 
		for(i=0,j=0;i<=len;i++)
		{
			if(s[i]==' ')
			{
				printf(" ");
				i++;
			}
			if(s[i]>='a' && s[i]<='z')
			{
				while(s[i]>='a' && s[i]<='z' && i<len)
				{
					s2[j]=s[i];
					j++; i++;
				}
				s2[j]='\0';//字符串结束符 
				j=0;
				//执行此函数时,说明文章中的该单词已经分离出来,开始进入Search()查找替换 
				Node *r=Search(s2);
				//如果返回的结点不为空,且该节点的loc[0]也不是 #,则说明此处存的有该单词对应替换词 
				if(r!=NULL && r->loc[0]!='#') 
					printf("%s",r->loc);
				else
					printf("%s",s2);
			}
			if(!(s[i]>='a' && s[i]<='z'))//此处输出标点符号 
				printf("%c",s[i]);
		}
	}
	return 0;
}


你可能感兴趣的:(hdu 1075 Trie树)