数据结构与算法课程作业--1014. Translation

这道题思想很简单,就是用map将foreign的作为键值,english的值作为对应的映射值,然后通过直接用foreign作为map对象的下标直接查找。

本题比较烦人的一点就是输入数据,我使用了getline(cin, **),getline的用法就是可以将空格也一起输入,默认情况下当读到换行符,就你按了回车键之后,表示输入结束,当然你也可以设定当读到某个字符时结束,如getline(cin, str, ' ')表示读到空格时结束。本题当输入空行时结束输入,这时就要在while,输入时用:while(getline(cin, line) && line.size() != 0)来控制。因为按了空行之后(回车符)getline结束,此时输入0个字符,空行不输入,所以line.size() == 0

 
     
 
 
1014. Translation
 
 
Total: 1061 Accepted: 208
 
     
     
 
Time Limit: 1sec    Memory Limit:256MB
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
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> Copy sample input to clipboard
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay
Sample Output
cat
eh
loops

Problem Source: 课程上机练习题

 
     
     
 
Submit

代码:

 // Problem#: 8661
// Submission#: 2494832
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<map>
#include<string>
#include<stdio.h>
using namespace std;
int main() {
map<string, string> dict;
string english;
string foreign;
string line;
while (getline(cin, line) && line.size() != ) {
int blank = line.find(' ');
foreign = line.substr(blank+);
english = line.substr(, blank);
dict[foreign] = english; }
while (cin >> foreign) {
if (dict[foreign] != "")
cout << dict[foreign] << endl;
else
cout << "eh" << endl;
}
return ;
}

题目链接:http://soj.me/show_problem.php?pid=1014&cid=1092

上一篇:【荐】PHP采集工具curl快速入门教程


下一篇:Middleware开发入门