hash
POJ 1840 Eqs
http://poj.org/problem?id=1840
Description
Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.
Input
The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.
Output
The output will contain on the first line the number of the solutions for the given equation.
Sample Input
37 29 41 43 47
Sample Output
654
解题思路:
如果暴力的枚举for要进行5层循环,不妨给等式变换一下形势;
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ==》a1x13+ a2x23= -( a3x33+ a4x43+ a5x53)
将等式左边的所有情况的可能值和次数算出来,这个时候就要用到hash表进行存储;
在将等式右边的所有可能情况与左边相加=0的情况都统计出来,计数
代码如下:
View Code
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define N 20000 #define mod 19993 struct Node { int num, count; struct Node *next; }*hash[N]; void Insert(int value) //插入hash表中 { int hashplace; if(value>0) hashplace=value%mod; //求的hash地址 else hashplace=-value%mod; Node *temp=hash[hashplace]; while(temp!=NULL) //该地址冲突检查该地址 { if(temp->num==value) //该地址的元素和该元素相等 { temp->count++; break; } temp=temp->next; } if(temp==NULL) //不冲突的地址,开辟以新的空间 { temp=new Node; temp->count=1, temp->num=value; temp->next=hash[hashplace]; hash[hashplace]=temp; } } void Left(int a, int b) { int i, j; for(i=-50; i<=50; i++) { if(i==0) continue; for(j=-50; j<=50; j++) { if(j==0) continue; int t=a*i*i*i+b*j*j*j; Insert(t); } } } int Find(int value) { int hashplace; if(value>0) hashplace=value%mod; else hashplace=-value%mod; Node *temp=hash[hashplace]; while(temp!=NULL) { if(temp->num==value) return temp->count; temp=temp->next; } return 0; } int Right(int a, int b, int c) { int i, j, k, tt=0; for(i=-50; i<=50; i++) { if(i==0) continue; for(j=-50; j<=50; j++) { if(j==0) continue; for(k=-50; k<=50; k++) { if(k==0) continue; int t=a*i*i*i+b*j*j*j+c*k*k*k; tt+=Find(t); } } } return tt; } int main() { int a, b, c, d, e; while(scanf("%d%d%d%d%d", &a, &b, &c, &d, &e)!=EOF) { memset(hash, 0, sizeof(hash)); Left(a, b); printf("%d\n", Right(c, d, e)); } return 0; }
POJ 2503 Babelfish
http://poj.org/problem?id=2503
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
解题思路:
给出一系列字典a和b串,在查找b串所对应的a串的单词是什么?如果没有输出eh;
此题用到一个hash函数ELF直接套模板;
将串b求出hash地址,放在该地址下的节点中,节点中的num对应的是串的标号,根据标号索引出串a;
代码如下:
View Code
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; char ch1[100002][12], ch3[100002][12]; #define M 199993 struct Node { int num; struct Node *next; }*hash[M]={NULL}; int ELF(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() { char ch2[22]; int t=0, i, j; struct Node *p; while(gets(ch2)) { if(strlen(ch2)==0) break; for(i=0; i<strlen(ch2); i++) { if(ch2[i]==' ') break; ch1[t][i]=ch2[i]; } int tt=0; for(j=i+1; j<strlen(ch2); j++) ch3[t][tt++]=ch2[j]; int hashplace=ELF(ch3[t]); p=new Node; p->num=t; p->next=hash[hashplace]; hash[hashplace]=p; t++; } while(gets(ch2)) { if(strlen(ch2)==0) break; int hashplace=ELF(ch2); p=hash[hashplace]; while(p!=NULL) { if(strcmp(ch2, ch3[p->num])==0) break; p=p->next; } if(p==NULL) printf("eh\n"); else printf("%s\n", ch1[p->num]); } return 0; }