1. 程式人生 > >【POJ 2503】 Babelfish

【POJ 2503】 Babelfish

complex amp style AR CM break rcm print 字符串

【題目鏈接】

http://poj.org/problem?id=2503

【算法】

字符串哈希

【代碼】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include 
<cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include
<istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include
<limits.h> using namespace std; #define MAXN 100010 typedef unsigned long long ULL; const int P = 13331; const int MOD = 1e5 + 7; int i,tot,t; ULL f[15]; char s[15],s1[MAXN][15]; int nxt[MAXN],Head[MOD],d[MAXN]; char english[MAXN][15]; char tmp; inline ULL get(char *s) { int i; ULL ret = 0; for (i = 0; i < strlen(s); i++) ret = ret * P + (s[i] - a + 1); return ret; } inline void Insert(ULL x,int p) { int pos = (int)(x % MOD); tot++; nxt[tot] = Head[pos]; d[tot] = p; Head[pos] = tot; } inline void query(char *s) { int i; ULL Hash = get(s); int pos = (int)(Hash % MOD); for (i = Head[pos]; i; i = nxt[i]) { if (strcmp(s,s1[d[i]]) == 0) { printf("%s\n",english[d[i]]); return; } } printf("eh\n"); } int main() { f[0] = 1; for (i = 1; i <= 12; i++) f[i] = f[i-1] * P; while (true) { t++; if (t == 1) scanf("%s%s",&english[t],&s1[t]); else { english[t][0] = tmp; scanf("%s%s",&s,&s1[t]); for (i = 0; i < strlen(s); i++) english[t][i+1] = s[i]; } Insert(get(s1[t]),t); getchar(); tmp = getchar(); if (tmp == \n) break; } while (scanf("%s",&s) != EOF) query(s); return 0; }

【POJ 2503】 Babelfish