有关字符串的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. WMI问题终极解决

    1. 如果用JInterOp,先用这篇文章叙述的: http://m.oschina.net/blog/73163 2.另外开启这个: https://dev.c-ware.de/confluence ...

  2. Eclipse图标含义

    学习了这么久,之前也没注意,这次在csdn找个了文章,记录一下: Eclipse的Package Explorer中用图标表示了很多内容,刚刚开始接触Eclipse时对这些图标表示的内容并不清楚,而且 ...

  3. Map map=new HashMap(); 为什么是这样

    Map是接口,hashMap是Map的一种实现.接口不能被实例化. Map map=new HashMap(); 就是将map实例化成一个hashMap.这样做的好处是调用者不需要知道map具体的实现 ...

  4. ios 开发指南

    苹果Xcode帮助文档阅读指南 iOS开发官方文档汇总 翻译"iPhone SDK 开发"之UIKit使用… iOS中arc的设置与使用

  5. TCP 协议三次握手过程解析带实例

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  6. poj1458 dp入门

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37551   Accepted: 15 ...

  7. Android 解析内存泄漏

    1.引用没释放造成的内存泄露 1.1.注册没取消造成的内存泄露        这种Android的内存泄露比纯Java的内存泄露还要严重,因为其他一些Android程序可能引用我们的Anroid程序的 ...

  8. Android中绘制圆角矩形图片及任意形状图片

    圆角矩形图片在苹果的产品中很流行,相比于普通的矩形,很多人都喜欢圆角矩形的图片,因为它避开了直角的生硬,带来更好的用户体验,下面是几个设计的例子: 下面在Android中实现将普通的矩形图片绘制成圆角 ...

  9. MySQL初始化故障-----mysql_config_editor中的坑

    今天准备新启一个MySQL实例,结果竟然无法初始化,内容如下: -------------------------------------------------------------------- ...

  10. Asp.Net长文件名下载的问题和解决办法

    在Asp.Net中写了一个附件上传和下载的程序,附件上传到数据库中,然后将附件的GUID保存起来,我们可以根据GUID来找到数据库中的附件,一般附件下载的代码是: <!--<br /> ...