题目大意
输入一些单词对,一行是一个英语单词然后一个空格,后面接一个翻译之后的单词。之后给出一些查询,输入翻译之后的单词,让你输出所对应的英语单词。
样例
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslayatcay
ittenkay
oopslaySample Output
cat
eh
loops
分析
这道题思路很直接,可以用hash表,也可以对字符串排序后二分查找,想省事一点map也是可以过的,效率应该是hash>排序二分>map.我感觉用以后能用hash的还是少用map。
做这种题最麻烦的是输入的处理,因为一行有两个单词,如果用cin必然太慢,又由于如果用scanf(“%s”)会忽略空格和回车不知道什么时候单词对结束,所以最后选择用getchar先一个字母一个字母输入放到数组最后加个’\0’,读入空格后再用scanf(“%s”)输入单词对的后一个单词。
代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
const int INF=999999999;
map<string,int> H;
char a[100005][30];
char b[100];
int main()
{
int pos=1;
char c;
int cnt=0;
while(1)
{
c=getchar();
if(c=='\n')break;
if(c==' ')
{
a[pos][cnt]='\0';
scanf("%s",b);
getchar();
H[b]=pos;
pos++;
cnt=0;
continue;
}
a[pos][cnt++]=c;
}
char x[30];
while(scanf("%s",x)!=EOF)
{
if(H[x]==0)printf("eh\n");
else printf("%s\n",a[H[x]]);
}
}
//a