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
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;
}