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".
题意:先输入多行字符串(相当于字典),每行字符串有两个单词,后一个单词是相对于前一个英语单词的另一种外国单词,接下来输入多个外国单词,拿每个单词与字典去查询,如果能查询到就输出英语单词;如果查询不到就输出“eh”。
思路:这个题其实有多种算法,弱这种渣渣目前就会用map算法2333,等明天学会了线段树再来更新多种算法。
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
char english[15],foreign[15];
map<string,string>trans; //键值为string,值也为string
map<string,bool>appear; //键值为string,值为bool型
int main()
{
//freopen("oo.text","r",stdin);
char t;
int i;
while(1)
{
t=getchar();
if(t=='\n') //要处理好换行和空格。如果此时是换行则说明字典已输完
break;
else
{
english[0]=t;
i=1;
while(1)
{
t=getchar();
if(t==' ') //如果此时是空格说明英语已经输完,接下来应该输外语
break;
english[i++]=t;
}
english[i]='\0';
}
cin>>foreign; //对于map里的string,必须用cin输入,如果用scanf的话,要将输入的字符数组转换成string
getchar(); //吃掉每个外语后的换行符
appear[foreign]=true; //如果出现了就标记上
trans[foreign]=english; //让键值为foreign的值为english
}
char word[15];
while(cin>>word) //切记用cin输入
{
if(appear[word])
cout<<trans[word]<<endl;
else
cout<<"eh"<<endl;
}
return 0;
}