POJ2503 Babelfish

题目:http://poj.org/problem?id=2503

题目大意:就是后面字符串映射前面字符串,然后查询输出。直接map,不过也可以考虑字典树解决;

AC代码:

#include<iostream>
#include<map>
#include<cstdio>
#include<string>
using namespace std;

string s, s1, s2;
map<string, string> mp;
int main() {
	while(getline(cin, s) && s.length()) {
			int flag = 0;
			for(int i = 0; s[i]; i++) {
				if(s[i] == ' ') {
					flag = 1;
				} else if(flag == 0) {
					s1 += s[i];
				} else s2 += s[i];
			}  //分离字符串 
			mp[s2] = s1;//后面映射前面 
			s1.clear();
			s2.clear();
	}
	while(cin >> s) {
		if(mp[s] != "")//如果映射不为空 
			cout << mp[s] << endl;
		else
			cout << "eh" << endl;
	}
}

 

posted @ 2018-08-03 10:42  Frontierone  阅读(111)  评论(0编辑  收藏  举报