zoukankan      html  css  js  c++  java
  • 课程题目:1014. Translation

     题目:

    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

    思路:

    题目总的思路比较简单,用STL里面的map容器作为数据结构存储单词,并用“foreign language"作为键值key进行索引,把读取到的数据以<string,string>形式存进map里面。

    一开始的时候对于getline函数不太熟悉,用了一个比较慢的办法,每次用getchar读取每行的首字母,如果是' '则表明是换行符。然后依次读进第一个单词的剩余字母,每个字母生成一个string类型然后加在原单词末尾。用getchar的时候要注意读取最后多余的换行符。

    后来问了师兄才懂了用getline函数,getline(cin,line)可以读取一行字符串到string类型的line里,读到空行时line.length()为0可以跳出循环。然后用空格的位置作为标记将空格左右两边分为两个单词。

    下面第一个代码为getchar,第二个代码为用了getline函数后的

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<map>
     4 #include<string>
     5 using namespace std;
     6 
     7 int main(){
     8     char c;//用来读取每行的首字母,用来检测空行
     9     map<string,string>dictionary;
    10     c=getchar();
    11     while(c!='
    '){//读到空行时停止
    12         string English,Foreign;
    13         while(c!=' '){//读完首个单词
    14             string a_letter(1,c);
    15             English=English+a_letter;
    16             c=getchar();
    17         }
    18         cin>>Foreign;
    19         dictionary[Foreign]=English;
    20         c=getchar();//读取多余换行符
    21         c=getchar();
    22     }
    23     string Foreign;
    24     map<string,string>::iterator it;
    25     while(cin>>Foreign){
    26         it=dictionary.find(Foreign);//找不到键值时it将指向map的end()
    27         if(it!=dictionary.end())
    28             cout<<it->second<<endl;
    29         else
    30             cout<<"eh
    ";
    31     }
    32     return 0;
    33 }
     1 #include<iostream>
     2 #include<map>
     3 using namespace std;
     4 
     5 int main(){
     6     map<string,string>dictionary;
     7     string line;
     8     while(getline(cin,line)&&line.length()!=0){ //读到空行时跳出循环
     9         //把一行从空格分开为左边和右边两个单词
    10         int blank=line.find(' ');
    11         string English=line.substr(0,blank);
    12         string Foreign=line.substr(blank+1);
    13         dictionary[Foreign]=English;
    14     }
    15     string Foreign;
    16     while(cin>>Foreign){
    17         if(dictionary[Foreign].length()==0){
    18             cout<<"eh
    ";
    19         }else{
    20             cout<<dictionary[Foreign]<<endl;
    21         }
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    无线安全课堂:手把手教会你搭建伪AP接入点
    转载——开阔自己的视野,勇敢的接触新知识
    关于系统架构的一些总结
    MessageBox.Show()如何换行
    不患寡而患不均
    由CHAR(2)引发的BUG
    DataRow.RowState 属性
    C# 使用TimeSpan计算两个时间差
    利用反射调出其他项目的界面
    DB2 中将date类型的转换成timestamp
  • 原文地址:https://www.cnblogs.com/jolin123/p/3374943.html
Copyright © 2011-2022 走看看