天天看點

poj2503 Babelfish

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<stack>
#define N 200000
#include<math.h>
#include<queue>
#define ll long long
//#define maxn  18750000
using namespace std;
char c;
char dic_h[100010][12];///   人類字典
char dic_e[100010][12];///   外語字典
int pos=0;
vector<int>hs[500];///  哈希
void insert_my(int n){///  插入下标
int lenth=strlen(dic_e[n]);
int key=0;
for(int i=0;i<lenth;i++){
    key+=(dic_e[n][i]-'A');
}
key%=500;
hs[key].push_back(n);
}
void search_my(char *s){///  搜尋
int lenth=strlen(s);
int key=0;
for(int i=0;i<lenth;i++){
    key+=(s[i]-'A');
}
key%=500;
for(int j=0;j<hs[key].size();j++){
    if(strcmp(s,dic_e[hs[key][j]])==0){
        printf("%s\n",dic_h[hs[key][j]]);
        return ;
    }
}
printf("eh\n");
}
int main(){
//freopen("input2.txt","r",stdin);
while(1){
    scanf("%s",dic_h[pos]);
    c=getchar();
    if(c!=' ')break;///   如果不是空格那麼結束
    scanf("%s",dic_e[pos]);
    insert_my(pos);
    ++pos;
}
char str[12];
search_my(dic_h[pos]);///  将之前那個while執行一半的搜尋
while(scanf("%s",str)!=EOF){///  一邊輸入一邊搜尋
    search_my(str);
}
return 0;
}
           

繼續閱讀