Hduoj1075【字典树】

/*What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 15270    Accepted Submission(s): 4903


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


Recommend
We have carefully selected several similar problems for you:  1800 1671 1247 1298 1016 
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Tree
{
	char s[11];
	bool ok;
	Tree * next[26];
}root;
char str[3010];
void build(char *s1, char *s2)
{
	int i, j, k, id, l;
	Tree *p = &root, *q;
	l = strlen(s2);
	for(i = 0; i < l; ++i)//put in
	{
		id = s2[i] - 97;
		if(p->next[id] == NULL)
		{
			q = (Tree *) malloc(sizeof(root));
			for(j = 0; j < 26; ++j)
				q->next[j] = NULL;
			q->ok = false;
			p->next[id] = q;
			p = q; 
		}
		else
		p = p->next[id];
	} 
	l = strlen(s1);
	for(i = 0; i < l; ++i)
	p->s[i] = s1[i];
	p->s[i] = '\0';
	p->ok = true;
}
void find(char *s1)
{
	int i, l, id;
	l = strlen(s1); 
	Tree *p = &root;
	for(i = 0; i < l; ++i)
	{
		id = s1[i] - 97;
		if(p->next[id] == NULL)
		break;
		else
		p = p->next[id]; 
	}
	if(i == l && p->ok)
	{
		int temp = strlen(p->s);
		for(i = 0; i < temp; ++i)
		printf("%c", p->s[i]);
	}
	else
	{
		for(i = 0; i < l; ++i)
		printf("%c", s1[i]); 
	}
}
int main()
{
	int i, j, k;
	char s1[11], s2[11];
	for(i  = 0; i < 26; ++i)
		root.next[i] = NULL;
	while(scanf("%s", s1) != EOF)
	{
		if(strcmp(s1, "START") == 0)
		continue;
		if(strcmp(s1, "END") == 0)
		break;
		scanf("%s", s2);
		build(s1, s2);
	}
	getchar();
	while(gets(str) != NULL)
	{
		if(strcmp(str, "START") == 0)
		continue;
		if(strcmp(str, "END") == 0)
		break;
		k = strlen(str);
		s2[0] = '\0';
		for(i = 0;i < k; ++i)
		{
			if(str[i] >= 97 && str[i] <= 122)
			{
				for(j = i; j < k; ++j)
				{
					if(str[j] >= 97 && str[j] <= 122)
					{
						s2[j-i] = str[j];
					}
					else
					{
						s2[j-i] = '\0';
						i = j;
						break;
					}
				}
				if(j == k)
				{
					s2[j-i] = '\0';
					i = j;
				} 
				find(s2);
				if(i != k) 
				printf("%c",str[i]);//结束处的字符 
			}
			else
			printf("%c", str[i]);
		} 
		printf("\n");
	}
	return 0;
} 


题意:给出一部分字典,再给出一段文章,分别都以“START”开始“END”结束,要求将文章中的单词翻译成字典中的单词,若是找不到则不翻译,对于空白字符和标点符号原样输出,求翻译后的文章。

思路:由于这题数据非常大,所以需要用字典树来保存字典,并且在翻译时查找单词也更加快。对于文章中的单词必须是连续的小写字母,所以可以根据这个来划分单词,然后查找输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值