10月15日 字典树(Babelfish)

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 32988 Accepted: 14189
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


Hint


Huge input and output,scanf and printf are recommended.
Source


Waterloo local 2001.09.22


做这一题特地从头到尾学习了字典树,不过没想到牵扯最多时间的是那个空行(gets还是第一次用,vs2015还必须用gets_s,听说是改了新标准)

字典树是个比较直观的东西,跟英文字典差不多(不过想想换成中文字典的话…难怪中文到处不方便)

这题我蠢了,因为不熟悉gets这些函数的用法,开了一些本可以合并的数组,将就看一下~

#include<cstdio>
#include<cstring>
int engnum;//当前英文表位置
char engdic[100000][11];
struct node {
	int s;//表示对应单词存储在第几个字符串,为-1就是没有
	node *next[26];//孩子
};
node* createnode()
{
	node *p = new node;
	for (int i = 0; i < 26; i++)
		p->next[i] = NULL;
	p->s = -1;
	return p;
}
void inserttree(node *p, char *s, char *eng)
{
	node *q = p;
	int len = strlen(s);
	for (int j = 0; j < len; j++)
	{
		int k = s[j] - 'a';
		if (q->next[k] == NULL)
		{
			q->next[k] = createnode();
		}
		q = q->next[k];
	}
	q->s = engnum++;
	len = strlen(eng);
	for (int j = 0; j < len; j++)
	{
		engdic[engnum-1][j] = eng[j];
	}
}
int findtree(node *p, char *s)
{
	int len = strlen(s);
	node *q = p;
	for (int i = 0; i < len; i++)
	{
		int k = s[i] - 'a';
		if (q->next[k] == NULL)return -1;
		q = q->next[k];
	}
	int code = q->s;
	return code;
}
int main()
{
	node *head = createnode();
	int i = 0, j = 0;
	char p1[100000][11], p2[100000][11], s[50];
	while (1)
	{
		gets_s(s);
		if (s[0] == '\0')break;
		sscanf(s, "%s%s", p1[i], p2[i]);
		inserttree(head, p2[i], p1[i]);
		i++;
	}
	while (gets_s(s) != NULL)
	{
		if (s[0] == '\0')break;
		int m = findtree(head, s);
		if (m == -1)
			printf("%s\n", "eh");
		else
			printf("%s\n", engdic[m]);
		j++;
	}
	delete head;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_viceversa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值