POJ-2503 Babelfish---map或者hash
题目链接:
https://vjudge.net/problem/POJ-2503
题目大意:
就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10)
解题思路:
map容器可以直接过,不过为了练习hash,写了个hash也可以过
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<sstream>
#define lowbot(i) (i&(-i))
//#define Rotate(a, b) node(a.x + a.y - b.y, a.y + b.x - a.x)
using namespace std;
typedef long long ll;
const int maxn = + ; const int mod = ;//一般为靠近总数的素数
struct Hashtable
{
string s, t;//hash存的值
Hashtable * next;
Hashtable()
{
next = ;
}
};
Hashtable * Hash[mod];
void Hash_Insert(string s, string t)//s对应t
{
int key = ;
for(int i = ; i < s.size(); i++)
key = (key * + (s[i] - 'a')) % mod;
if(!Hash[key])//该key第一个元素
{
Hashtable * p = new Hashtable;
p->s = s;
p->t = t;
Hash[key] = p;
}
else
{
Hashtable *p = Hash[key];
while(p->next)p=p->next;
Hashtable* temp = new Hashtable;
temp->s = s;
temp->t = t;
p->next = temp;
}
}
void Find(string s)
{
int key = ;
for(int i = ; i < s.size(); i++)
key = (key * + (s[i] - 'a')) % mod;
if(Hash[key])
{
Hashtable * temp = Hash[key];
while(temp)
{
if(temp->s == s)
{
cout<<temp->t<<endl;
return;
}
temp = temp->next;
}
}
cout<<"eh"<<endl;
return;
} int main()
{
string s, s1, s2;
while(getline(cin, s))
{
if(s.size() == )break;
stringstream ss(s);
ss >> s1 >> s2;
Hash_Insert(s2, s1);
}
while(cin >> s2)
{
Find(s2);
}
return ;
}
POJ-2503 Babelfish---map或者hash的更多相关文章
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- Poj 2503 Babelfish(Map操作)
一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- poj 2503 Babelfish (查找 map)
题目:http://poj.org/problem?id=2503 不知道为什么 poj 的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...
- 题解报告:poj 2503 Babelfish(map)
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- poj 2503 Babelfish(字典树或着STL)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35828 Accepted: 15320 Descr ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- POJ 2503 Babelfish
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28766 Accepted: 12407 Descripti ...
- POJ 2503 Babelfish (STL)
题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...
随机推荐
- wireshark开发环境搭建
自己完成了wireshark开发环境的搭建,主要参考资料是wireshark的官方developer-guide.pdf,网址:https://www.wireshark.org/docs/. 现把搭 ...
- python : No such file or directory
windows上写的python脚本,在linux上执行时报: No such file or directory 解决方法一# sed -i 's#\r##' mysqlchk.py 解决方法二脚本 ...
- 阿里云服务器mail 命令发邮件
一.申请开通25端口 https://yundun.console.aliyun.com/?spm=5176.2020520001.aliyun_topbar.188.KbmgKc&p=sc# ...
- css 断行省略号,隐藏,fixed定位
text-overflow(clip | elipsis)(显示省略号| 不显示省略号) white-space:nowrap 强制文字不断行 word-break:break-all; ...
- linux-centos-pgsql-Ident authentication failed for user “postgres”错误出现解决方法
首先,要找到pg_hba.conf\ -->cd /var/lib/pgsql/data -->vi pg_hba.conf 然后,将里面的配置文件修改如下: # TYPE DATABAS ...
- jquery 操作 checkbox select
1.prop方法获取.设置checked属性 <input type="checkbox" name="checkboxMain" onclick=&qu ...
- Murano Weekly Meeting 2015.09.08
Meeting time: 2015.September.8th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting su ...
- (转)使用介质设备安装 AIX 以通过 HMC 安装分区
使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...
- xftp的使用教程
使用xftp来上传,下载文件到linux主机 首先,我们要下载一个xftp,因为官网是英文的,还需要邮件激活,在这里我把程序下载好 此时已经安装完成,点击finish,打开软件登录SSH账号,这里以默 ...
- 动态资源不缓存 filter
package com.itheima.web.filter; import java.io.IOException; import javax.servlet.Filter; import java ...