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 ...
随机推荐
- 解决IDEA卡顿问题及相关基本配置
https://blog.csdn.net/u013068377/article/details/54316965 https://blog.csdn.net/u014527619/article/d ...
- asp.net core 标签验证函数功能
public class AuthFilter : Attribute, IActionFilter { public void OnActionExecuted(ActionExecutedCont ...
- 问题:git处理中文名称时候显示为编码形式(已解决)
问题描述: Untracked files: (use "git add <file>..." to include in what will be committed ...
- java——注解Annotation
http://www.cnblogs.com/xdp-gacl/p/3622275.html 元注解: @Retention:当 @Retention 应用到一个注解上的时候,它解释说明了这个注解的的 ...
- robotframework使用过程中的若干问题
问题0: 在ride上import library,显示红色解决办法:根据菜单栏tools--view ride log提示依次安装缺少的模块,包括natsort.xlrd.xlwd.xlutils, ...
- 在ubuntu中安装mysql及简单操作方式
老规矩,ctrl+alt+t呼出终端, 输入 sudo apt-get update 更新源,否则在运行下面代码时会报出有几个包无法下载,你还是得回来执行这句代码, 输入 sudo apt-get i ...
- Lock接口和ReadWriteLock接口
Lock接口 Lock接口在java.util.concurrent.locks包中,在jdk1.5之后才有. Lock接口有6个方法: void lock(); void lockInterrupt ...
- python Gensim库建立word2vec参数说明
from gensim.models import word2vec model = word2vec.Word2Vec(sentences, size=80, window=10,workers=6 ...
- 贪心:钱币找零问题(C++)
贪心是一种算法范例,它一点一点地构建解决方案,总是选择下一个提供最明显和最直接好处的部分.因此,选择局部最优也会导致全局解的问题最适合贪心问题. 例如,考虑分数背包问题.局部最优策略是选择权重比最大的 ...
- .NET控制台程序监听程序退出
There are mainly 2 types of Win32 applications, console application and window application. They hav ...