POJ-2503 Babelfish---map或者hash

题目链接:

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 = 1000 + 10;
16
17 const int mod = 99973;//一般为靠近总数的素数
18 struct Hashtable
19 {
20     string s, t;//hash存的值
21     Hashtable * next;
22     Hashtable()
23     {
24         next = 0;
25     }
26 };
27 Hashtable * Hash[mod];
28 void Hash_Insert(string s, string t)//s对应t
29 {
30     int key = 0;
31     for(int i = 0; i < s.size(); i++)
32         key = (key * 26 + (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 = 0;
53     for(int i = 0; i < s.size(); i++)
54         key = (key * 26 + (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() == 0)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 0;
87 }

原文地址:https://www.cnblogs.com/fzl194/p/8949783.html

时间: 2024-11-10 15:45:46

POJ-2503 Babelfish---map或者hash的相关文章

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

题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码(Map实现): #include <iostream> #include <sstream> #include <string> #include <map> using namespace std; int main() { char word[15], fo

poj 2503 Babelfish (map,trie 树)

链接:poj 2503 题意:输入 语言A及翻译为语言B的词典,之后再输入语言B的单词,判断是否能从词典中找到, 若能找到,将其翻译为语言A,否则输出"eh". 思路:这题肯定得先将词典对应语言存起来,但是如果直接暴力找输入的单词是否出现过,必然会TLE 因为单词都是一对一的关系,可以用map实现 当然,trie树是用空间换时间,对于字符串的查找,在时间上有着相当的优势,因此也可以用trie树 注:sscanf函数,从一个字符串中读进与指定格式相符的数据. map实现:938MS #i

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

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

POJ 2503 Babelfish(map入门)

题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<string> #include<map> #include<iostream> using namespace std; int main(void){ char english[11],foreign[11]; map<string,bool>appear; //记录foreign与english的配对映射是否出

题解报告:poj 2503 Babelfish(map)

Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them. Input Input consists of up to 100,000 dictionary ent

poj 2503 Babelfish

题目链接:http://poj.org/problem?id=2503 题目大意:就是给你一本词典,问你能否在词典中找到你要查询单词的意思,不能就输出eh 思路:map的入门级题,直接词典中的词组存到map中,然后直接查询.就是有些细节需要注意 code: #include<cstdio> #include<iostream> #include<cmath> #include<string> #include<map> using namespa

POJ 2503 Babelfish(字典树)

题目链接:http://poj.org/problem?id=2503 题意:翻译单词,若在词典中找不到则输出eh. 思路:裸的字典树. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #include <vector> using

poj 2503 Babelfish(字典树或着STL)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

POJ 2503 Babelfish qsort+bserach

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36951   Accepted: 15743 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have