题目链接:

https://vjudge.net/problem/POJ-2503

题目大意:

就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10)

解题思路:

map容器可以直接过,不过为了练习hash,写了个hash也可以过

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<map>
  6. #include<set>
  7. #include<cmath>
  8. #include<algorithm>
  9. #include<vector>
  10. #include<sstream>
  11. #define lowbot(i) (i&(-i))
  12. //#define Rotate(a, b) node(a.x + a.y - b.y, a.y + b.x - a.x)
  13. using namespace std;
  14. typedef long long ll;
  15. const int maxn = + ;
  16.  
  17. const int mod = ;//一般为靠近总数的素数
  18. struct Hashtable
  19. {
  20. string s, t;//hash存的值
  21. Hashtable * next;
  22. Hashtable()
  23. {
  24. next = ;
  25. }
  26. };
  27. Hashtable * Hash[mod];
  28. void Hash_Insert(string s, string t)//s对应t
  29. {
  30. int key = ;
  31. for(int i = ; i < s.size(); i++)
  32. key = (key * + (s[i] - 'a')) % mod;
  33. if(!Hash[key])//该key第一个元素
  34. {
  35. Hashtable * p = new Hashtable;
  36. p->s = s;
  37. p->t = t;
  38. Hash[key] = p;
  39. }
  40. else
  41. {
  42. Hashtable *p = Hash[key];
  43. while(p->next)p=p->next;
  44. Hashtable* temp = new Hashtable;
  45. temp->s = s;
  46. temp->t = t;
  47. p->next = temp;
  48. }
  49. }
  50. void Find(string s)
  51. {
  52. int key = ;
  53. for(int i = ; i < s.size(); i++)
  54. key = (key * + (s[i] - 'a')) % mod;
  55. if(Hash[key])
  56. {
  57. Hashtable * temp = Hash[key];
  58. while(temp)
  59. {
  60. if(temp->s == s)
  61. {
  62. cout<<temp->t<<endl;
  63. return;
  64. }
  65. temp = temp->next;
  66. }
  67. }
  68. cout<<"eh"<<endl;
  69. return;
  70. }
  71.  
  72. int main()
  73. {
  74. string s, s1, s2;
  75. while(getline(cin, s))
  76. {
  77. if(s.size() == )break;
  78. stringstream ss(s);
  79. ss >> s1 >> s2;
  80. Hash_Insert(s2, s1);
  81. }
  82. while(cin >> s2)
  83. {
  84. Find(s2);
  85. }
  86. return ;
  87. }

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. 数据层——ImageData层

    layer { name: "data" type: "ImageData" top: "data" top: "label&qu ...

  2. java——字典树 Trie

    字典树是一种前缀树 package Trie; import java.util.TreeMap; public class Trie { private class Node{ public boo ...

  3. 研磨设计模式学习笔记4--单例模式Signleton

    需求:加载配置文件,由于配置文件全局唯一,所以不用过多对象,建一个就可以了. 优点:单例模式本质就是为了控制实例数目. 一.饿汉式 public class Singleton { private S ...

  4. cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤

    真实验证有效:点击链接 查看具体移植过程.

  5. python Gensim库建立word2vec参数说明

    from gensim.models import word2vec model = word2vec.Word2Vec(sentences, size=80, window=10,workers=6 ...

  6. Matrix Chain Multiplication (堆栈)

    题目链接:https://vjudge.net/problem/UVA-442 题目大意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. 假定A是m*n的矩 ...

  7. Murano Weekly Meeting 2015.10.20

    Meeting time: 2015.October.20th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting sum ...

  8. jdk 动态代理 数据连接池

    package com.itheima.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandl ...

  9. 分布式数据库sort那些事儿

    待填. 收回之前的填坑时间. 计划永远没有变化快,所有周末都奉献上还是有干不完的活,待闲时再来填..

  10. 【安全测试】sql注入

    SQL注入攻击是黑客对 数据库 进行攻击的常用手段之一,随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员越来越多,但是由于程序员水平及经验页参差不齐,相当大部分程序员在编写代码的时候没有 ...