Babelfish UVA - 10282

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86506115

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

题目不难,也很容易懂。。用map搞一下就可以了

但是写这个题的时候学到了一个函数sscanf,所以写了这篇博客。

C 库函数 int sscanf(const char *str, const char *format, ...) 从字符串读取格式化输入。

给一个实例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   int day, year;
   char weekday[20], month[20], dtm[100];

   strcpy( dtm, "Saturday March 25 1989" );
   sscanf( dtm, "%s %s %d  %d", weekday, month, &day, &year );

   printf("%s %d, %d = %s\n", month, day, year, weekday );
    
   return(0);
}

编译之后的结果:

March 25, 1989 = Saturday

写这个题的时候又一次尝试了unordered_map, 却是比普通的map快不少!

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e5 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const int seed = 133 ;

char s[Maxn] ;
char s1[Maxn], s2[Maxn] ;
unordered_map < string , string > uma ;

int main (){
    while (gets(s)){
        if (s[0] == '\0') break ;
        sscanf(s, "%s%s", s1, s2) ;
        uma[s2] = s1 ;
    }
    while (gets(s)){
        if (s[0] == '\0') break ;
        if (uma.find(s) != uma.end()) cout << uma[s] << endl ;
        else cout << "eh" << endl ;
    }
    return 0 ;
}

问君能有几多愁

恰似一江春水向东流

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86506115