有关字符串的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. qt 4.6.2 vs 2005 + QCreator 开发环境配置(有注册码)

    配置开发环境可真是个痛苦的过程,网上的资料参差不齐,只有自己一步步来试验一下了 本人环境 virtualbox +  xp +  vs 2005  en 1.安装vs 2005 en sp1 下载vs ...

  2. linux下维护服务器之常用命令

    linux下维护服务器之常用命令! 第1套如下: 正则表达式: 1.如何不要文件中的空白行和注释语句: [root@localhost ~]# grep -v '^$' 文件名 |grep -v '^ ...

  3. JavaEE Tutorials (25) - 使用Java EE拦截器

    25.1拦截器概述380 25.1.1拦截器类381 25.1.2拦截器生命周期381 25.1.3拦截器和CDI38125.2使用拦截器381 25.2.1拦截方法调用382 25.2.2拦截生命周 ...

  4. Python中and和or

    转自:http://unei66.blog.163.com/blog/static/544640292010320745886/ python中的and和or 4.6. and 和 or 的特殊性质在 ...

  5. 学习本课程需要具备哪些基础及微信小程序目录结构介绍

    1.html css js 基础 2.ajax 基础 3.简单的面向对象基础  

  6. E6全部刷机包

    此版本号基于R533_G_11.11.10P_GSZMCAUT679DA01B_LP064DA_T679DA_S005_E001_P002_R001_G004_1FF.sbf制作耳机接听或挂机正常内置 ...

  7. 【最大团】【HDU1530】【Maximum Clique】

    先上最大团定义: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题,在国际上已有广泛的研究,而国内对MCP问题的研究则还处于起步 ...

  8. java 判断字符串编码

    String iso8859 = new String(sb.toString().getBytes("iso8859-1"));String gbk = new String(s ...

  9. [代码]Java后台推送消息到IOS前端

    PayLoad payLoad = new PayLoad(); payLoad.addAlert("test");    //手机端的提示消息 payLoad.addBadge( ...

  10. Sql遍历数据库

    Sql遍历数据库 set nocount on ) ) ) set @str='ad' Declare cur_Depart Cursor For select name,id from syscol ...