C++-POJ2503-Babelfish[hash]
哈个希加挂个链表
一个要背的字符串hash函数ELFhash()
mod数取数据最大容量的1.5倍最佳?!
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=;//比1e5大的质数
struct node{char e[],f[];int next;}word[MAXN];
int cnt,hashHead[MAXN];char s[]; int ELFhash(char *key){
unsigned long h=;
for(unsigned long g;*key;h&=~g){
h=(h<<)+(*key++);
g=h&0Xf0000000L;
if(g)h^=g>>;
}
return h%MAXN;
} void find(){
int hash=ELFhash(s);
for(int i=hashHead[hash];i;i=word[i].next)
if(strcmp(s,word[i].f)==)
{printf("%s\n",word[i].e);return;}
printf("eh\n");
} int main() {
for(cnt=;gets(s)&&s[]!='\0';cnt++/*cnt++必须写在外面*/) {
sscanf(s,"%s%s",word[cnt].e,word[cnt].f);
int hash = ELFhash(word[cnt].f);
word[cnt].next=hashHead[hash];//挂表
hashHead[hash]=cnt;
}
while(gets(s))find();
return ;
}
C++-POJ2503-Babelfish[hash]的更多相关文章
- POJ2503——Babelfish(map映射+string字符串)
Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...
- POJ2503 Babelfish map或者hash_map
POJ2503 这是一道水题,用Map轻松AC. 不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列. 自己生成一个质数, 随便搞一下. #include&l ...
- POJ2503(Babelfish)--简单字典树
思路:就是用一个字典树翻译单词的问题,我们用题目中给出的看不懂的那些单词建树,这样到每个单词的叶子结点中存放原来对应的单词就好. 这样查询到某个单词时输出叶子结点存的就行,查不到就"en&q ...
- POJ2503——Babelfish
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- POJ2503 Babelfish
题目链接. 分析: 应当用字典树,但stl的map做很简单. #include <iostream> #include <cstdio> #include <cstdli ...
- poj_2503(map映射)
题目链接poj2503 Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 38820 Accepted: ...
- POJ2503(hash)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 41263 Accepted: 17561 Descr ...
- POJ-2503 Babelfish---map或者hash
题目链接: https://vjudge.net/problem/POJ-2503 题目大意: 就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10) 解题思路: ma ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
随机推荐
- 剑指offer-面试题14-剪绳子-贪婪算法
/* 题目: 给定一个长度为n的绳子,把绳子剪为m段,(n>1,m>1) 求各段绳子乘积的最大值. */ /* 思路: 贪婪算法. 当绳子的长度大于5时,尽可能多的剪长度为3的绳子:当剩下 ...
- P6070 [RC-02] GCD [杜教筛,莫比乌斯反演]
没啥好说的,杜教筛板子题. \[\sum_{i=1}^{N} \sum_{j=1}^{N}\sum_{p=1}^{\lfloor \frac{N}{j} \rfloor}\sum_{q=1}^{\lf ...
- 安装Kibana到Linux(源码)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:Kibana-7.1.0 硬件要求:最低2核4GB 安装过程 1.源码安装JDK 1.1.从官网 ...
- LeetCode 965. 单值二叉树 (遍历二叉树)
题目链接:https://leetcode-cn.com/problems/univalued-binary-tree/ 如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树. 只有给定的树是 ...
- 【终端使用】常用Linux命令的基本使用
常用Linux命令的基本使用: 命令 对应英文 作用 ls list 查看当前文件夹下的内容 pwd print work directory 查看当前所在的文件夹 cd [目录名] change d ...
- SMTP模块发送邮件
import os import smtplib # 处理多种形态的邮件主体我们需要 MIMEMultipart 类 from email.mime.multipart import MIMEMult ...
- Visual Studio Code搭建Python开发环境方法总结
更新:目前VSCode官方Python插件已经支持代码运行与调试,无需安装Code Runner插件. 1.下载安装Python,地址 https://www.python.org/downloads ...
- java注释英语自动机翻
一.简介 每次面试都被怼spring源码你看过吗?你用spring开发源码你都不看的吗?这样怎么开发?那我就下点决心,趁着现在疫情在家宅,我要看spring源代码.但是发现注释都是英文,勉勉强强能看懂 ...
- 第一章 - HTTP概述
1.1 HTTP——因特网的多媒体信使 可靠的数据传输协议 1.2 Web客户端和服务器 Web内容都是存储在Web服务器上的,使用HTTP协议,因此也称为HTTP服务器 HTTP客户端发出请求,提供 ...
- 牛客网——剑指offer(跳台阶以及变态跳台阶_java实现)
首先说一个剪枝的概念: 剪枝出现在递归和类递归程序里,因为递归操作用图来表示就是一棵树,树有很多分叉,如果不作处理,就有很多重复分叉,会降低效率,如果能把这些分叉先行记录下来,就可以大大提升效率——这 ...