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 ...
随机推荐
- 数据层——ImageData层
layer { name: "data" type: "ImageData" top: "data" top: "label&qu ...
- java——字典树 Trie
字典树是一种前缀树 package Trie; import java.util.TreeMap; public class Trie { private class Node{ public boo ...
- 研磨设计模式学习笔记4--单例模式Signleton
需求:加载配置文件,由于配置文件全局唯一,所以不用过多对象,建一个就可以了. 优点:单例模式本质就是为了控制实例数目. 一.饿汉式 public class Singleton { private S ...
- cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤
真实验证有效:点击链接 查看具体移植过程.
- python Gensim库建立word2vec参数说明
from gensim.models import word2vec model = word2vec.Word2Vec(sentences, size=80, window=10,workers=6 ...
- Matrix Chain Multiplication (堆栈)
题目链接:https://vjudge.net/problem/UVA-442 题目大意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. 假定A是m*n的矩 ...
- Murano Weekly Meeting 2015.10.20
Meeting time: 2015.October.20th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting sum ...
- jdk 动态代理 数据连接池
package com.itheima.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandl ...
- 分布式数据库sort那些事儿
待填. 收回之前的填坑时间. 计划永远没有变化快,所有周末都奉献上还是有干不完的活,待闲时再来填..
- 【安全测试】sql注入
SQL注入攻击是黑客对 数据库 进行攻击的常用手段之一,随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员越来越多,但是由于程序员水平及经验页参差不齐,相当大部分程序员在编写代码的时候没有 ...