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之 ...
- Win10磁盘占用率过高
打开服务 禁用Superfetch 禁用Windows Search 禁用Connected User Experiences and Telemetry 禁用Windows Update[恢复选项卡 ...
- Linux下Tomcat启动关闭命令
1.首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 2.查看Tomcat是否以关闭 ps -ef|grep tomcat 如果显示以下信息,说明Tomcat还没 ...
- kmspico
# process | 在这儿找到了原作者的地址 http://blog.nsfocus.net/kmspico/ | 下面是原作者地址 https://forums.mydigitallife.ne ...
- 关于C语言读取文件时候多读一行
梗概:为什么C语言读取文件到结构体时为什么整天多读一行?一切都是[!feof(fp)]的错!!! while (!feof(fp)) { fgets(buffer, , fp); j++; } 像这样 ...
- Linux raw socket
转载自:http://www.cnblogs.com/uvsjoh/archive/2012/12/31/2840883.html 我们平常所用到的网络编程都是在应用层收发数据,每个程序只能收到发给自 ...
- 关于数学问题的urls
一个知乎账号, 分析了很多的数学问题: https://www.zhihu.com/people/matongxue/activities 关于三阶样条的解析: https://blog.csdn.n ...
- POJ 1861 ——Network——————【最小瓶颈生成树】
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15268 Accepted: 5987 Specia ...
- 二叉排序树思想及C语言实现
转自: http://blog.chinaunix.net/uid-22663647-id-1771796.html 1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找( ...
- c++ 控制台输入参数
#include <iostream>#include <string> using namespace std; int main(int argc,char **argv) ...