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

本文介绍了一种简单的翻译程序设计思路,利用map数据结构实现英语到外语的词汇映射,并通过逐行读取输入来处理字典条目和待翻译的消息。

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

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

 

课程上机练习题

Contest ends in 8 months 27 days
 
Xieyz3 ss12330344
You havn't any signature yet.
Logout
 
   
 
 
1014. Translation
 
 
Total:1061Accepted: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
 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

代码:

 1 // Problem#: 8661
 2 // Submission#: 2494832
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include<iostream>
 7 #include<map>
 8 #include<string>
 9 #include<stdio.h>
10 using namespace std;
11 int main() {
12     map<string, string> dict;
13     string english;
14     string foreign;
15     string line;
16     while (getline(cin, line) && line.size() != 0) {
17           int blank = line.find(' ');
18           foreign  = line.substr(blank+1);
19           english = line.substr(0, blank);
20           dict[foreign] = english;
21           
22     }
23     while (cin >> foreign) {
24           if (dict[foreign] != "")
25               cout << dict[foreign] << endl;
26           else
27               cout << "eh" << endl;
28     }
29     return 0;
30 }                                 

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

转载于:https://www.cnblogs.com/xieyizun-sysu-programmer/p/3456797.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值