poj 2503 Babelfish (字符串hash +sscanf读入技巧)

题目链接

题意:给定一个两种语言的对照关系表...给出后一种语言中的单词,问对应的前一种语言的单词是什么。。。

思路:hash一下然后map存一下即可。。。。读入方式由于单词表和查询是根据空行分开的。。那么读入不能用scanf(因为会跳过空行),要用gets。。。然后再sscanf一下。。。

/* ***********************************************
Author :111qqz
Created Time :2016年11月20日 星期日 11时13分29秒
File Name :code/poj/2503.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E5+7;
 7char eng[N][12],fori[N][12];
 8char str[N];
 9map<int,int>mp;
10unsigned int BKDRHash(char *str)
11{
12    unsigned int seed = 113;
13    unsigned int hash = 0 ;
14    while (*str) hash = hash*seed+(*str++);
15    return (hash&0x7fffffff);
16}
 1int main()
 2{
 3	#ifndef  ONLINE_JUDGE 
 4	freopen("code/in.txt","r",stdin);
 5  #endif
 6    int cnt = 1 ;
 7    while (gets(str))
 8    {
 9	if (strlen(str)==0) break;
10	sscanf(str,"%s %s",eng[cnt],fori[cnt]);
11	cnt++;
12    }
13    cnt--;
14 //   for ( int i = 1; i <= cnt ;i++) printf("%s %s\n",eng[i],fori[i]);
15    for ( int i = 1; i <= cnt ; i++) mp[BKDRHash(fori[i])] = i ;
16    while(scanf("%s",str)!=EOF)
17    {
18	if (!mp[BKDRHash(str)]) puts("eh");
19	else puts(eng[mp[BKDRHash(str)]]);
20    }
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}