题目
POJ传送门
Vjudge传送门
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
代码
#include<iostream>
#include<sstream>
using namespace std;
#define FRE freopen("1.txt", "r", stdin)
#define IOS ios::sync_with_stdio(false)
const int N = 5e6 + 10;
int tr[N][26], tot = 0;
int End[N], num = 1;
void insert(string s){
int pos = 0;
for(int i = 0; i < s.length(); i++) {
int c = s[i] - 'a';
if(tr[pos][c] == 0) {
tr[pos][c] = ++tot;
}
pos = tr[pos][c];
}
End[pos] = num++;
return;
}
int query(string s){
int pos = 0;
for(int i = 0; i < s.length(); i++){
int c = s[i] - 'a';
if(tr[pos][c] == 0){
return 0;
}
pos = tr[pos][c];
}
return End[pos];
}
string str[100005];
int main(int argc, char** argv){
IOS;
int slen = 1;
string ss;
while(getline(cin, ss)){
if(ss.empty()) break;
istringstream sin(ss);
sin >> ss;
str[slen++] = ss;
sin >> ss;
insert(ss);
}
while(cin >> ss){
int ans = query(ss);
if(ans == 0) cout << "eh" << endl;
else cout << str[ans] << endl;
}
return 0;
}