您的位置:首页 > 其它

POJ 2503 Babelfish (Trie树)

2016-07-20 23:00 441 查看
Babelfish

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 40025 Accepted: 17052
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 entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears
more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output
cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.

Trie树的一道裸题。。。

用STL的map貌似也能水过,不过要效率差上不少,一旦时间卡的紧就超了。所以还是有必要学会自己撸个字典树。

Trie树又称单词查找树,字典树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

#include <cstring>
#include <cstdio>
#include <string>
using namespace std;

const int MAXN = 1e5 + 10;
int n = 0;
char word[MAXN][15];
char _map[MAXN][15];

struct Trie {
int v;
Trie* child[26];

Trie() {
v = -1;
memset(child, 0, sizeof(child));
}
} *root;

void Insert(char* str, int v) {
Trie* p = root;
while (*str) {
if (p->child[*str - 'a'] == NULL) {
p->child[*str - 'a'] = new Trie;
}
p = p->child[*str - 'a'];
++str;
}
p->v = v;
}

string Find(char* str) {
Trie* p = root;
while (*str) {
if (p->child[*str - 'a'] == NULL) {
return "eh";
}
else {
p = p->child[*str - 'a'];
++str;
}
}
return p->v != -1 ? _map[p->v] : "eh";
}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
root = new Trie;
char cmd, query[15];
while (scanf("%s%c", _map
, &cmd), cmd == ' ') {
scanf("%s", word
);
Insert(word
, n);
++n;
}

printf("%s\n", Find(_map
).c_str());
while (scanf("%s", query) != EOF) {
printf("%s\n", Find(query).c_str());
}
return 0;
}

特地写了一个STL版本,对比了一下,发现STL的map虽然慢了点,但是内存上大大领先,所以实际要看情况使用吧

情况对比 stl map / Trie



使用map的代码如下:

#include <cstring>
#include <cstdio>
#include <string>
#include <map>
using namespace std;

const int MAXN = 1e5 + 10;
map<string, string> ma;

void Put(map<string, string>::iterator it) {
if (it == ma.end()) {
puts("eh");
}
else {
puts(it->second.c_str());
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
char sa[15], sb[15], cmd;
while (scanf("%s%c", sa, &cmd), cmd == ' ') {
scanf("%s", sb);
ma.insert(make_pair(sb, sa));
}
Put(ma.find(sa));
while (scanf("%s", sa) != EOF) {
Put(ma.find(sa));
}
return 0;
}


当然如果OJ编译器支持c++11以上的话,还可以用unordered_map,这个效率比map快上不少。

不过遗憾的是POJ不支持C++11,没办法实践了。代码和map的使用方法一样,只是把map改成unordered_map就行了

#include <cstring>
#include <cstdio>
#include <string>
#include <unordered_map>
using namespace std;

const int MAXN = 1e5 + 10;
unordered_map<string, string> ma;

void Put(unordered_map<string, string>::iterator it) {
if (it == ma.end()) {
puts("eh");
}
else {
puts(it->second.c_str());
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
char sa[15], sb[15], cmd;
while (scanf("%s%c", sa, &cmd), cmd == ' ') {
scanf("%s", sb);
ma.insert(make_pair(sb, sa));
}
Put(ma.find(sa));
while (scanf("%s", sa) != EOF) {
Put(ma.find(sa));
}
return 0;
}


2017.2更,封装

typedef pair<int, int> pii;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int mod = 142857;
const int maxn = 1e5 + 9;

struct Node {
int cnt;
Node* next[26];
Node() : cnt(0) {
memset(next, 0, sizeof next);
}
~Node() {
for (int i = 0; i < 26; ++i)
if (next[i]) delete next[i];
}
};

struct Trie {
Node* const root;
Trie() : root(new Node()) {}

void Insert(char* str) {
Node* p = root;
while (*str) {
if ((*p).next[*str - 'a']) {
p = (*p).next[*str - 'a'];
} else {
p = (*p).next[*str - 'a'] = new Node();
}
(*p).cnt++;
++str;
}
}

int Query(char* str) {
Node* p = root;
while (*str && (*p).next[*str - 'a']) {
p = (*p).next[*str - 'a'];
++str;
}
if (*str) return 0;
else return (*p).cnt;
}

void Clear() {
delete root;
}
};

int main() {
//std::ios::sync_with_stdio(0);
//std::cin.tie(0);
#ifdef NIGHT_13
freopen("in.txt", "r", stdin);
//freopen("myout.txt", "w", stdout);
#endif
char str[100] = {};
Trie trie = Trie();

int n = ReadInt();
while (n--) {
scanf("%s", str);
trie.Insert(str);
}
int m = ReadInt();
while (m--) {
scanf("%s", str);
printf("%d\n", trie.Query(str));
}
trie.Clear();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm poj