题目链接:

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的更多相关文章

  1. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  2. Poj 2503 Babelfish(Map操作)

    一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...

  3. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

  4. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

  5. poj 2503 Babelfish(字典树或map或哈希或排序二分)

    输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...

  6. 题解报告:poj 2503 Babelfish(map)

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  7. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...

  8. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  9. POJ 2503 Babelfish

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28766 Accepted: 12407 Descripti ...

  10. POJ 2503 Babelfish (STL)

    题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...

随机推荐

  1. 如何查看mysql执行的所有SQL

    在程序调试中,有时需要看到最终在DB执行的SQL文,而默认mysql此功能是关闭的,开启的方法如下: set global general_log='ON'; 然后用如下命令查看log文件所在路径即可 ...

  2. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  3. Windows下 virtualenv 使用

    Windows下 virtualenv 使用 win python virtualenv 首先在电脑上安装两个不同版本的python mkvirtualenv --python C:\Python34 ...

  4. LBS开发

    功能:用户发送自动的位置,返回周围的厕所信息 思路:根据用户的经纬度信息,调用百度地图的api,查询周围的厕所位置并且返回! 步骤:进入百度地图官网注册账号,选择web api接入 我们先看开发者文档 ...

  5. my.兽决_等_价格

    1.20170411 音乐洒水车,升50级 送了 兽决 隐身,摆摊推荐价格 20000金,大家都卖26000金 2.20170417 音乐洒水车 挖到 必杀 魔决,推荐价格 19820金,我以 -10 ...

  6. js 中callback函数的定义和使用

    这是js里的解释了,其他语言的算我没说. 字面上理解下来就是,回调就是一个函数的调用过程.那么就从理解这个调用过程开始吧.函数a有一个参数,这个参数是个函数b,当函数a执行完以后执行函数b.那么这个过 ...

  7. Map集合练习之对字符串中字母出现的次数求和

    不多说,直接上干货! 代码需求 如有这么一个字符串 String str = "fdg+avAdc bs5dDa9c-dfs"; MapTest.java package zhou ...

  8. linux命令strings

    linux命令strings,其man信息如下:strings(1)                                        GNU Development Tools      ...

  9. Ubuntu上的相关问题

    一.解决Ubuntu中vi命令的编辑模式下不能正常使用方向键和退格键的问题 在Ubuntu中,进入vi命令的编辑模式,发现按方向键不能移动光标,而是会输出ABCD,以及退格键也不能正常删除字符.这是由 ...

  10. 参数化拼接in查询条件,个人备份

    /// <summary>        /// 查询数据报表        /// </summary>        /// <param name="Tr ...