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 ...
随机推荐
- rest_framework 的验证,权限,频率
回到顶部 快速实例 Quickstart 回到顶部 序列化 创建一个序列化类 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之 ...
- spark 2.X 疑难问题汇总
当前spark任务都是运行在yarn上,所以不用启动长进程worker,也没有master的HA问题,所以主要的问题在任务执行层面. 作业故障分类故障主要分为版本,内存和权限三方面. - 各种版本不一 ...
- sqlserver 数据库阻塞和死锁
参考原文:http://blog.csdn.net/ha196200/article/details/44985597 (1) 数据库阻塞: 假设第一个连接T1占有且没有释放资源,第二个连接T2请求同 ...
- NETCore 调试
https://www.cnblogs.com/MingQiu/p/8227644.html https://www.cnblogs.com/shumin/p/9967854.html 前言 core ...
- Docker & ASP.NET Core 教程
第一篇:把代码连接到容器 第二篇:定制Docker镜像 第三篇:发布镜像 第四篇:容器间的连接 第五篇: Docker & ASP.NET Core (5):Docker Compose AS ...
- mkpasswd的使用
首先安装except包:yum -y install except 参数: -l # (密码的长度定义, 默认是 9) -d # (数字个数, 默认是 2) -c # (小写字符个数, 默认是 2) ...
- python3 importlib模块简单利用
importlib作用:根据字符串形式导入模块,并且找到其中的类并执行 import importlib # m = importlib.import_module("src.plugins ...
- 移动测试之appium+python 简单例子(五)
# coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...
- db2 存储过程参数传递--字段类型转换产生的问题
修改之前的脚本 select count(*) from dbdk.dtdkg010 A left join DBDK.DTDKG070 D ON D.PAY_NO = A.PAY_NO LEFT J ...
- Myeclipse修改jdk版本流程
Myeclipse修改jdk版本流程 很多时候,项目没有用对jdk版本时候,项目报错,在MyEclipse中,要修改JDK版本 有三处地方需要注意:!! 1.第一处 2.第二处 3.第三处