Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 46675 | Accepted: 19560 |
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
问题链接:POJ2503 UVA10282 Babelfish。
问题描述:参见上文。
问题分析:
这个问题是一个字典问题,可以用map来实现,也可以用字典树来解决。问题的关键是时间上能否更快。
程序说明:
解法一是使用map:
本来是想用类unordered_map(采用哈希搜索的map)来编写程序,编译不支持,只好改为map。
这个问题用类unordered_map来编写程序,时间上会更快一些,也更为合理。
解法二是使用字典树:
需要对字典树进行简单的改造。
AC的C++语言程序(解法一:map)如下:
/* POJ2503 UVA1028 Babelfish */
#include <iostream>
#include <string>
//#include <unordered_map>
#include <map>
#include <sstream>
using namespace std;
int main()
{
// unordered_map<string, string> words;
map<string, string> words;
string line, first, second;
int i;
while (getline(cin, line)) {
if(line.length() == 0)
break;
istringstream sin(line);
sin >> first >> second;
words[second] = first;
}
while(getline(cin, line)) {
i = words.count(line);
if (i > 0)
cout << words[line] << endl;
else
cout << "eh" << endl;
}
return 0;
}
AC的C++语言程序(解法二:字典树)如下:
/* POJ2503 UVA10282 Babelfish */
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 100000;
const int LEN = 10;
const int SIZE = 26;
const char SCHAR = 'a';
struct Trie {
int acnt; // access count
int childs[SIZE];
char word[LEN];
void init()
{
acnt = 1;
memset(childs, 0, sizeof(childs));
word[0] = '\0';
}
} trie[N * LEN];
int ncnt; // Trie Node count
char s[LEN + 1]; // Input
char w[LEN + 1];
void insert(char s[], char w[])
{
int p = 0;
for(int i = 0; s[i]; i++) {
int k = s[i] - SCHAR;
int child = trie[p].childs[k];
if(child) {
trie[child].acnt++;
p = child;
} else {
trie[++ncnt].init();
if(s[i + 1] == '\0') // 改造
strcpy(trie[ncnt].word, w);
trie[p].childs[k] = ncnt;
p = ncnt;
}
}
}
int query(char s[])
{
int p = 0;
for (int i = 0; s[i]; i++) {
int k = s[i] - SCHAR;
int child = trie[p].childs[k];
if (child == 0)
return 0;
else
p = child;
}
// return trie[p].acnt;
return p;
}
int main()
{
char ss[LEN + LEN + 1];
while(gets(ss)) {
if(ss[0] == '\0')
break;
sscanf(ss, "%s%s", w, s);
insert(s, w);
}
while(gets(s)) {
int ans = query(s);
printf("%s\n", ans ? trie[ans].word : "eh");
}
return 0;
}