昨天在pku的的acm论坛上遇到这样的一道题:就是一个简单的字典问题。给定一组对应的英文和火星鸟语的字符串,并假设这些信息就构成一个字典。题目要求输入一个火星鸟语,然后经过查询,显示出相应的英语。(每个输入的英语或者火星鸟语都不得超过10个字节)
我个人觉得这道题,考查的就是对字符串的输入问题的处理。
题目给了一个提示:输入数据量较大,建议采用scanf和printf输入和输出数据。
Description
You have just moved from 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
注意在输入字典数据和查询数据之间有一个空行。(本题的输入关键处理部分)也是我写这篇随笔的用意。
#include<stdio.h>
#include<vector>
#include<iterator>
using namespace std;
struct dictionary
{
char flanguage[11];
char English[11];
};
int main()
{
vector<dictionary> d_array;
vector<dictionary>::iterator iter;
char s1[11],s2[11],find[11];
while(1)
{
dictionary temp; //识别回车的处理部分
s1[0]=getchar();
if(s1[0]=='\n')break;
scanf("%s %s",s1+1,s2);
strcpy(temp.English,s1);
strcpy(temp.flanguage,s2);
d_array.push_back(temp);
getchar();
}
while(scanf("%s",find)==1)
{
bool logic=true;
for(iter=d_array.begin();iter!=d_array.end();++iter)
{
if(!strcmp(iter->flanguage,find))
{
printf("%s\n",iter->English);
logic=false;
}
}
if(logic)
printf("eh\n");
}
return 0;
}
我个人脑子比较笨,想了半天想到的就只有这一种处理方法,如果大家有什么好的方法,希望你别太吝啬,不妨和大家分享你新奇的做法吧!