Babelfish
Time Limit: 3000MS | | Memory Limit: 65536K | Total Submissions: 46265 | | Accepted: 19415 |
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 题意: 题目还是比较容易读懂的 意思是给你一些key-value对 然后再给你value 让你判断value是否出现过 如果出现过输出key 否则输出eh 本来以为非要字典树才能过的 但是发现map一发也能过 先贴 一个map过的代码 map用的不是很熟 #include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <cstdio>
#include <map>
#include <iomanip>
using namespace std;
int cnt ;
map <string,string> MAP;
int main()
{
char temp[100];
while ( gets(temp) ) {
string str_first, str_second;
int length_temp = strlen(temp);
if (!temp[0]) break;
int pos = 0;
while (temp[pos] != ' ') {
str_first += temp[pos++];
}
pos ++;
while (pos != length_temp) {
str_second += temp[pos++];
}
MAP[str_second] = str_first;
}
string str;
while ( ~scanf("%s",temp) ) {
str = temp;
if (MAP.count(str)) {
cout << MAP[str] << endl;
} else {
cout << "eh" << endl;
}
}
} 这份代码如果交C++能比G++快2000ms+ 神奇 然后在贴一个字典树写的代码吧 字典树的思路还是比较简单的 每次都把value存起来 然后cnt就是边号 把这个边号当成哈希值来用 把对应的key按这个值存起来 然后在询问的时候数出来就好 字典树的这份能跑260ms左右 #include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10001100;
int tree[N][26] = {0};
bool nums[N] ;
int cnt ;
char key_value[N][11];
void Insert(char *value)
{
int length_value = strlen(value);
int i = 0;
int k = 0;
while (value[i]) {
int pos = value[i++] - 'a';
if (tree[k][pos] == 0)
tree[k][pos] = cnt ++;
k = tree[k][pos];
}
nums[k] = true;
}
int Find(char *p)
{
int length_p = strlen(p);
int i = 0;
int k = 0;
while (p[i]) {
int pos = p[i++] - 'a';
if (tree[k][pos] == 0)
return 0;
k = tree[k][pos];
}
return k;
}
int main()
{
char str_first[100];
char str_second[100];
char str[200];
cnt = 1;
memset(nums, false, sizeof(nums));
while (gets(str)) {
if (str[0] == 0) break;
sscanf(str, "%s %s", str_first,
str_second);
Insert(str_second);
strcpy(key_value[cnt-1] , str_first);
}
while (~scanf("%s",str)) {
int res;
res = Find(str);
if (!res) printf("eh\n");
else printf("%s\n",key_value[res]);
}
}
|