POJ 2503 Babelfish(map或hash或排序二分)

poj 2503

题目大意

输入一些单词对,一行是一个英语单词然后一个空格,后面接一个翻译之后的单词。之后给出一些查询,输入翻译之后的单词,让你输出所对应的英语单词。

样例

Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值