【代码超详解】HDU 1075 What Are You Talking About(字典树,452 ms)

这篇博客详细介绍了如何使用字典树(Trie)解决HDU 1075问题,帮助将火星历史书籍翻译成英文。文章包含题目描述、算法分析和AC代码,提供了对字典树应用的深入理解。
摘要由CSDN通过智能技术生成

传送门

一、题目描述

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 33833 Accepted Submission(s): 11668
(2020/2/13 20:24)

Problem Description

Ignatius is so lucky that he met a Martian yesterday. But he didn’t know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?

Input

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string “START”, this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian’s language. A line with a single string “END” indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string “START”, this string should be ignored, then an article written in Martian’s language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can’t find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(’ ‘), tab(’\t’), enter(’\n’) and all the punctuation should not be translated. A line with a single string “END” indicates the end of the book part, and that’s also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.

Output

In this problem, you have to output the translation of the history book.

Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
 

Sample Output

hello, i'm from mars.
i like earth!

Hint

Huge input, scanf is recommended.

Author

Ignatius.L

二、算法分析说明与代码编写指导

字典树(Trie)又称前缀树,是一种针对字符串匹配优化的数据结构。其根节点为空,每个节点都有若干指针指向下一个节点。按字符串查询时,从根节点开始(已匹配 0 位),每深入一层代表已匹配待查找字符串多一位。
字典树携带的信息可以保存在中间的非叶节点上,也可以保存在叶节点上。
注意:字典树的每个单词的末尾要添加结束标记,否则在部分题目中会出错:当查找的单词虽然不在字典树中存在,但是是某个已存在的单词的前缀时,如果不通过专门的标记来判定该前缀的末尾实际上并非一个单词的结束,那么就会返回查找的单词存在的错误结果。
在编写本题的代码时,字典树的模板要稍作修改。
首先在节点的定义中添加一个 char s[11] 用于保存每个火星文单词对应的英语单词。当然,非叶节点的 s 永远为空,只有叶节点的 s 才可能非空且具有实际意义。为了配合题目给的 在词典中找不到对应的英语单词时输出原火星文单词 的要求,find 成员函数要改成在查找不到时返回原单词 w,查找成功则返回字典树中该叶节点保存的单词 s。
不停读取字符,直到遇到非英文字母时查找刚才已经记录的单词,根据查找结果选择输出原火星文单词或对应的英文单词。
在本代码中,输入的单词保存到 w 的第 p 个位置,p 随着读入而递增。读到第一个非英文字母后,会先用字符 c 保存该非英文字母,然后将 w[p] 置为 0(字符串结束标记),这样就可以直接传递 w 在字典树上检索。检索并输出单词后,输出读到的第一个非英文字母 c 并将 p 置零,继续读入字符。
非英文字母肯定是不会保存到字典树中的,所以本代码能顺利直接输出标点符号、空格和回车。

三、AC 代码(452 ms)

#include<cstdio>
#include<map>
#include<cstring>
#include<cctype>
#pragma warning(disable:4996)
class trie {
private:
	struct node { std::map<char, node*> next; bool end = false; char s[11]; };
	std::map<char, node*>::iterator I; std::pair<char, node*> P;
	node* root;
public:
	trie() { root = new node(); }
	void insert(const char* const k, const char* const v) {
		node* p = root; const char* q = k;
		while (*q != 0) {
			I = p->next.find(*q);
			if (I == p->next.end()) {
				P.first = *q; P.second = new(node)(); p->next.emplace(P); I = p->next.find(*q);
			}
			p = I->second, ++q;
		}
		p->end = true; strcpy(p->s, v);
	}
	const char* find(const char* const w) {
		node* p = root; const char* q = w;
		while (*q != 0) {
			I = p->next.find(*q);
			if (I == p->next.end()) { return w; }
			p = I->second, ++q;
		}
		if (p->end == false)return w;
		return p->s;
	}
};
trie t; char s1[11], s2[11], w[3001], c; unsigned p;
int main() {
	scanf("%s", s1);
	for (;;) {
		scanf("%s", s2); if (s2[0] == 'E')break;
		scanf("%s", s1); t.insert(s1, s2);
	}
	scanf("%s", s1); getchar();
	for (;;) {
		for (;;) {
			w[p] = getchar();
			if (isalpha(w[p]) == false) {
				if (w[0] == 'E')return 0;
				c = w[p]; w[p] = 0; fputs(t.find(w), stdout);
				putchar(c); p = 0; continue;
			}
			++p;
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值