POJ2503 Babelfish(AC 字典树)

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 44750 Accepted: 18893
Description


You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. 
Fortunately, you have a dictionary to help you understand them.
Input


Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. 
Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word 
appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. 
Each word in the input is a sequence of at most 10 lowercase letters.
Output


Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated 
as "eh".




Sample Input


dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay


atcay
ittenkay
oopslay
Sample Output


cat
eh

loops

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>

#define MAXL 15
#define ZIMU 26

char word[MAXL];
char diction[MAXL];
char inputword[MAXL];
char ans[MAXL];

typedef struct node
{
	char word[MAXL]; //如果是代表一个单子,这里面放的就是单子
	int isword;
	node* next[ZIMU];//这个是表示链表
}nodes;

nodes* head;

int strlength(char *s)
{
	int size = 0;
	while ('\0' != *s)
	{
		s++;
		size += 1;
	}
	return size;
}

void maketree()
{
	//开始建立字典树
	int wordlen = strlength(word);
	int diclen  = strlength(diction);
	int i = 0;
	int j = 0;
	int index = 0;
	node* p = head;
	for (i = 0; i < diclen;i++)
	{
		index = diction[i] - 'a';
		//从head开始找
		if (NULL == p->next[index])
		{
			//之前没有这个字母,那就要创建
			p->next[index] = (nodes*)malloc(sizeof(nodes));
			p->next[index]->isword = 0;
			for (j = 0; j < MAXL; j++)
			{
				p->next[index]->word[j] = '\0';
			}
			for (j = 0; j < ZIMU; j++)
			{
				p->next[index]->next[j] = NULL;
			}
		}

		p = p->next[index]; //p就是下一个点
		if (i == (diclen - 1)) //最后一个字母了,那就要把代表的单子记下来
		{
			p->isword = 1;
			for (j = 0; j < wordlen;j++)
			{
				p->word[j] = word[j];
			}
		}
	}

	return;
}

void findtree()
{
	int i = 0;
	node* p = head;
	int index = 0;
	int wordlen = strlength(inputword);
	//开始查单词,一个一个开始查
	for (i = 0; i < wordlen;i++)
	{
		index = inputword[i] - 'a';
		if (NULL ==  p->next[index])
		{
			sscanf("eh","%s", ans);
			return;
		}
		else
		{
			if (i != (wordlen - 1))
			{
				p = p->next[index];
			}
		}

		if (i == (wordlen - 1))
		{
			if (1 != p->next[index]->isword)
			{
				sscanf("eh", "%s", ans);//没有单词匹配
				return;
			}
			else
			{
				sscanf(p->next[index]->word, "%s", ans);
				return;
			}
		}
	}
}

int main()
{
	char str[50];
	int i = 0;
	int j = 0;
	freopen("input.txt","r",stdin);
	//while ((scanf("%s %s", &word, &diction)) && ('\0' == word[0]))
	//先把所有的字符串读进来,然后判断是否是空行
	/*gets(s)函数与scanf("%s",s)相似,但不完全相同,使用scanf("%s",s) 函数输入字符串时存在一个问题,
	  就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个
	  字符串直到遇到换行为止*/
	//字典树根节点没有字母的
	head = (nodes*)malloc(sizeof(nodes));
	head->isword = 0;
	for (j = 0; j < MAXL; j++)
	{
		head->word[j] = '\0';
	}
	for (j = 0; j < ZIMU; j++)
	{
		head->next[j] = NULL;
	}

	while (gets(str))
	{
		if ('\0' == str[0])
		{
			break;
		}
		sscanf(str, "%s%s", &word, &diction);
		maketree();
	}

	//while ((2 == scanf("%s %s", &word, &diction)) && ('\0' != word[0])) //不能这么输入,这么输入空行之久就跳过了
	
	while ((1 == scanf("%s", &inputword)) && ('\0' != inputword[0])) //这个后面没有空行,可以这么写
	{
		findtree();
		printf("%s\n", ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值