有关字符串的hash,用黑书上推荐的传说中的ELFhash函数

输入的话,用sscanf处理比較简洁

#include<iostream>
#include<cstring>
using namespace std;
#define M 9991
struct n1
{
char foreigh[11],english[11];
n1 *next;
};
n1 hash[M];
int ELFhash(char *key)
{
long long g,h=0;
while(*key)
{
h=(h<<4)+*key++;
g=h&0xf0000000L;
if(g)
h^=g>>24;
h&=~g;
}
return h%M;
}
void find(char *s)
{
n1 *p;
for(p=&hash[ELFhash(s)];p->next!=0;p=p->next)
if(strcmp(p->foreigh,s)==0)
{
printf("%s\n",p->english);
return;
}
printf("eh\n");
}
void insert(char *e,char *f)
{
n1 *p;
for(p=&hash[ELFhash(f)];p->next!=0;p=p->next);
strcpy(p->english,e);
strcpy(p->foreigh,f);
p->next=new(n1);
p->next->next=0;
}
void datain()
{
char tmp[22],english[11],foreigh[11];
while(gets(tmp)&&tmp[0])
{
sscanf(tmp,"%s %s",english,foreigh);
insert(english,foreigh);
}
}
void solve()
{
char foreigh[11];
memset(hash,0,sizeof(hash));
datain();
while(scanf("%s",foreigh)!=EOF)
find(foreigh);
}
int main()
{
solve();
}

poj2503的更多相关文章

  1. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  2. POJ-2503 Babelfish---map或者hash

    题目链接: https://vjudge.net/problem/POJ-2503 题目大意: 就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10) 解题思路: ma ...

  3. POJ2503字典树

    此代码原始出处:http://blog.csdn.net/cnyali/article/details/47367403 #include<stdio.h> #include<str ...

  4. POJ2503 Babelfish map或者hash_map

    POJ2503 这是一道水题,用Map轻松AC. 不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列. 自己生成一个质数, 随便搞一下. #include&l ...

  5. poj2503 哈希

    这题目主要是难在字符串处理这块. #include <stdio.h> #include <string.h> #include <stdlib.h> #defin ...

  6. POJ2503——Babelfish

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  7. POJ2503 Babelfish

    题目链接. 分析: 应当用字典树,但stl的map做很简单. #include <iostream> #include <cstdio> #include <cstdli ...

  8. POJ2503(hash)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 41263   Accepted: 17561 Descr ...

  9. 2017ecjtu-summer training #2 POJ2503

                                                                                                        ...

随机推荐

  1. C语言的本质(27)——C语言与汇编之计算机结构

    现代计算机都是基于冯·诺依曼或哈佛体系结构的,不管是嵌入式系统.个人电脑还是服务器.这种两种体系结构的主要特点是:CPU和内存是计算机的两个主要组成部分,内存中保存着数据和指令,CPU从内存中取指令执 ...

  2. C语言运算符的优先级

    熟悉C语言的同学都知道,C语言众多的运算符及繁琐难记的优先级总是搞得我们这些C初学者头大.那么本文就 对C语言中所有的运算符进行汇总,并对其优先级进行一定的介绍. 这里虽然对所有C运算符的优先级进行了 ...

  3. hdu 1010 Tempter of the Bone(dfs暴力)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. OC基础5:继承

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.根类即是最顶层的类,父类也可称为超类: ...

  5. CheckBoxPreference组件

    CheckBoxPreference 选中为true 取消选中为false 它的值会以boolean的形式储存在SharedPreferences中. <?xml version="1 ...

  6. Android布局(一)layout_gravity 属性和 gravity属性的区别

    安卓中的 layout_gravity 属性和 gravity属性 有啥区别? LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravi ...

  7. How to Install/Deinstall Oracle Workspace Manager (文档 ID 263428.1)

    In this Document   Goal   Solution   References APPLIES TO: Workspace Manager - Version 9.0.1.0 to 1 ...

  8. PhoneGap Xcode iOS教程

    http://mobile.51cto.com/web-334924.htmhttp://phonegap.com/install/http://www.phonegap100.com/jiaoche ...

  9. [IOS]图标尺寸

    最新参考网址:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Ico ...

  10. C# DropDownList绑定添加新数据的几种方法

    第一种:在前台手动绑定(适用于固定不变的数据项) <asp:DropDownList ID="DropDownList1" runat="server"& ...