POJ2503 哈希(字符串编码)
来源:互联网 发布:基于java博客 编辑:程序博客网 时间:2024/04/30 11:13
Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 37822 Accepted: 16111
Description
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
题意:
给出多个单词组成的字典,有几个查询,问每个查询是否能找到对应的单词。
题解:
这题用map也能水过,不过时间耗时太高。正确的做法是hash。把字符串编码为一个键值放到哈希表里,然后查询。常用的字符串编码都试了一遍,对应耗时标在编码方式上面。
#include <iostream>#include <algorithm>#include <cstdio>#include <map>#include <cstdlib>#include <cstring>#include <string>#define f(i,a,b) for(int i = a;i<=b;i++)#define fi(i,a,b) for(int i = a;i>=b;i--)#define M 100003#define HASH APhashusing namespace std;struct node{ int hash; struct node* next;}*link[M] = {NULL};char word[100000][11],dialect[100000][11],qu[11];unsigned int APhash(char* str){ unsigned int hash = 0; int i; for(i = 0;*str;i++) if((i&1)==0) hash^=((hash<<7)^(*str++)^(hash>>3)); else hash^=(~((hash<<11)^(*str)^(hash>>5))); return (hash&0x7FFFFFFF)%M;}// TLEunsigned int DJBhash(char* str){ unsigned int hash = 5381; while(*str){ hash += (hash<<5) + (*str); } return (hash&0x7FFFFFFF)%M;}// 688msunsigned int PJWhash(char* str){ unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int)*8); unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt*3)/4); unsigned int OneEighth = (unsigned int)(BitsInUnignedInt/8); unsigned int HighBits = (unsigned int)(0xffffffff)<<(BitsInUnignedInt - OneEighth); unsigned int hash = 0; unsigned int test = 0; while(*str){ hash = (hash<<OneEighth)+(*str++); if((test = hash&HighBits)!=0) hash = ((hash^=(test>>ThreeQuarters))&(~HighBits)); } return (hash&0x7FFFFFFF)%M;}// 688msunsigned int JShash(char* str){ unsigned int hash = 1315423911; while(*str){ hash^=((hash<<5)+(*str++)+(hash>>2)); } return (hash&0x7FFFFFFF)%M;}// 704msunsigned int RShash(char* str){ unsigned int b = 378551; unsigned int a = 63689; unsigned int hash = 0; while(*str){ hash = hash*a + (*str++); a*=b; } return (hash&0x7FFFFFFF)%M;}// 719msunsigned int SDBMhash(char* str){ unsigned int hash = 0; while(*str) hash = (*str++) + (hash<<6) + (hash<<16) - hash; return (hash&0x7FFFFFFF)%M;}// 688msunsigned int BKDRhash(char* str){ unsigned int seed = 1313131; //31 131 1313 13131 131313 unsigned int hash = 0; while(*str) hash = hash*seed + (*str++); return (hash&0x7FFFFFFF)%M;}// 641ms 常用 linux文件int ELFhash(char* key){ unsigned long h = 0,g; while(*key){ h = (h<<4) + *key++; g = h&0xf0000000L; if(g) h^=g>>24; h&=~g; } return h%M;}int main(){// freopen("data.in","r",stdin); int n = 0; struct node* p; while(1){ char c; c = getchar(); if(c == '\n') break; else word[n][0] = c; scanf("%s %s",word[n]+1,dialect[n]); getchar(); int e = HASH(dialect[n]); p = (struct node*) malloc(sizeof(struct node)); p->hash = n; p->next = link[e]; link[e] = p; n++; } while(1){ char c; c = getchar(); if(c == -1) break; else qu[0] = c; scanf("%s",qu+1); getchar(); int e = HASH(qu); p = link[e]; while(p!=NULL){ if(strcmp(qu,dialect[p->hash])==0) break; p = p -> next; } if(p == NULL) printf("eh\n"); else printf("%s\n",word[p->hash]); } return 0;}
- POJ2503 哈希(字符串编码)
- poj2503(字符串hash)
- poj2503 哈希
- Babelfish(poj2503字符串处理)
- POJ2503 字符串HASH
- poj2503字符串HASH
- poj2503 二分查找字符串
- POJ2503(二分,哈希)
- POJ2503 Babelfish(hash)
- POJ2503--(STLmap应用)
- poj2503(简单hash)
- POJ2503 Babelfish (map)
- POJ2503
- poj2503
- poj2503
- POJ2503
- poj2503
- poj2503
- Condition使用总结
- wamp You don't have permission to access / on this server等问题的解决.
- BaseAdapter和SimpleAdapter的区别
- Delphi 模式窗体返回值ModalResult的使用方法及注意事项
- 今天零碎学习的小知识点
- POJ2503 哈希(字符串编码)
- Ubuntu下搭建Qt+OpenCV开发环境
- hdfs的FileSystem API
- Qt中常用的对话框总结QDialog
- sqlite 句柄-sqlite 基础教程(3)
- 2015年11月之 有生之年
- WebKit之JSCell的继承关系图
- 工程中导入sqlite -sqlite 基础教程(1)
- java习题