博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hash
阅读量:5916 次
发布时间:2019-06-19

本文共 4778 字,大约阅读时间需要 15 分钟。

POJ 1840 Eqs

Description

Consider equations having the following form: 
a1x1
3+ a2x2
3+ a3x3
3+ a4x4
3+ a5x5
3=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
#include
#include
using namespace std;#define N 20000#define mod 19993struct 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

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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops 解题思路:

给出一系列字典a和b串,在查找b串所对应的a串的单词是什么?如果没有输出eh;

此题用到一个hash函数ELF直接套模板;
将串b求出hash地址,放在该地址下的节点中,节点中的num对应的是串的标号,根据标号索引出串a;

代码如下:

View Code
#include
#include
#include
using namespace std;char ch1[100002][12], ch3[100002][12];#define M 199993struct 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
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;}

转载于:https://www.cnblogs.com/Hilda/archive/2012/11/05/2755841.html

你可能感兴趣的文章
android 获取系统默认路径
查看>>
Highcharts API中英对照查询表
查看>>
Docker 网络管理
查看>>
repaint和reflow的相关知识
查看>>
android系统信息总结
查看>>
phantomjs处理异步加载后,获取渲染后的页面源码
查看>>
varnish 简介以及实用配置
查看>>
Python中pyc文件的用途
查看>>
eclipse调试远程Tomact
查看>>
[Swift]快速排序算法
查看>>
【致青春】开垦出的IT路
查看>>
四种方案解决ScrollView嵌套ListView问题
查看>>
实例讲解PAT配置
查看>>
centos下network和NetworkManager冲突的解决方法
查看>>
Mac OS上搭建Apache+PHP+MySQL开发环境的详细教程
查看>>
ccnp大型企业综合案例分析2
查看>>
odoo二次开发小知识点
查看>>
linux基础命令 rm
查看>>
多线程 -线程面试题(一)
查看>>
Java基础学习总结(1)——equals方法
查看>>