有关字符串的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. 100个linux站点

    (一) 文件下载 (二) 幽默娱乐 (三) 相关新闻 (四) 通用硬体 (五) 专用硬体 (六) 新手站点 (七) 图形/多媒体 (八) 游戏站点 (九) 网路杂志 (十) 入口(教育.链结) (十一 ...

  2. codevs1219 骑士游历

    题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象棋马. 规定: 1)马只能走日字 2)马只能向右跳 问给定起点x1,y1和终点x2,y ...

  3. Snippet Compiler——代码段编译工具

    原文地址:http://www.cnblogs.com/conexpress/archive/2011/07/24/2115308.html 不知道大家在工作中是否遇到过下面的情况:在项目中实现了一段 ...

  4. Oracle 表的常见操作

    --创建表并指定表空间 create table goods( id VARCHAR2(20) primary key, name VARCHAR2(20) NOT NULL, price NUMBE ...

  5. 优秀的目录文档内容查找,替换工具,可以飞快的帮助你查询大IIS日志哟。

    这,是一款飞速的目录文档中内容查找的工具. 它,飞快精准的帮助你查询到你想搜索的文档中的内容. 它,是一款由非常牛B,我都不晓得姓名的作者开发的,冒失是C++的windows应用. 你,非常需要他. ...

  6. jquery第三期:js与jquery对象转换

    我们开始进入jquery的学习了,jquery的学习就不那么中规中矩了,我们来看一个和javascript有所区别的地方. <!DOCTYPE html PUBLIC "-//W3C/ ...

  7. Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

    解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...

  8. 使用javascript oop开发滑动(slide) 菜单控件

    这里使用原生的javascript,用面向对象的方式创建一个容易维护使用方便的滑动菜单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...

  9. linux-FTP服务常用命令及测试

    一.vsftpd服务的安装,启动及关闭1.安装vsftpd[root@rusky bdump]# yum install vsftpd --必须配置yum源才能使用yum命令来安装vsftpd,或者挂 ...

  10. 【贪心】【TOJ4107】【A simple problem】

    Given three integers n(1≤n≤1018), m(1≤m≤105), k(1≤k≤1018). you should find a list of integer A1,A2,- ...