前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 648. 单词替换(Trie树)

LeetCode 648. 单词替换(Trie树)

作者头像
Michael阿明
发布2020-07-13 15:30:20
5640
发布2020-07-13 15:30:20
举报
文章被收录于专栏:Michael阿明学习之路

1. 题目

在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如,词根an,跟随着单词 other(其他),可以形成新的单词 another(另一个)。

现在,给定一个由许多词根组成的词典和一个句子。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根,则用最短的词根替换它。

你需要输出替换之后的句子。

代码语言:javascript
复制
输入: dict(词典) = ["cat", "bat", "rat"]
sentence(句子) = "the cattle was rattled by the battery"
输出: "the cat was rat by the bat"

注:
输入只包含小写字母。
1 <= 字典单词数 <=1000
1 <=  句中词语数 <= 1000
1 <= 词根长度 <= 100
1 <= 句中词语长度 <= 1000

2. Trie解题

参考:Trie树

  • 先将单词插入Trie树
  • 然后依次查询每个单词的各前缀是否在Trie中,进行替换
代码语言:javascript
复制
class TrieNode//节点
{
public:
	char ch;
	TrieNode *next[26];
	bool isEnd;
	TrieNode(char c = '/'):ch(c),isEnd(false) 
	{
		memset(next, 0, sizeof(TrieNode*)*26);
	}
};
class Trie//Trie树
{
public:
	TrieNode *root;
	Trie()
	{
		root = new TrieNode();
	}
	~Trie()//析构释放内存
	{
		destroy(root);
	}
	void destroy(TrieNode *root)
	{
		if(root == NULL)
			return;
		for(int i = 0; i < 26; i++)
			destroy(root->next[i]);
		delete root;
	}
	void insert(string str)//插入单词
	{
		TrieNode *cur = root;
		for(char s:str)
		{
			if(cur->next[s-'a'] == NULL)
				cur->next[s-'a'] = new TrieNode(s-'a');
			cur = cur->next[s-'a'];
		}
		cur->isEnd = true;
	}
};
class Solution {
	Trie tree;
public:
    string replaceWords(vector<string>& dict, string sentence) {
        for(string s:dict)
        	tree.insert(s);
        string word, ans, prefix;
        TrieNode *cur = tree.root;
        istringstream in(sentence);
        int i, chIdx;
        while(in >> word)
        {
        	cur = tree.root;
        	prefix = "";
        	for(i = 0; i < word.size(); ++i)
        	{
        		chIdx = word[i]-'a';
        		if(cur->next[chIdx] == NULL)
	        	{
	        		ans += word;
	        		break;
	        	}
	        	else
	        	{
	        		prefix.push_back(word[i]);
	        		if(cur->next[chIdx]->isEnd)
        			{
        				ans += prefix;
        				break;
        			}
                    if(i == word.size()-1)//最后一个字符特殊处理
        				ans += prefix;
	        	}
	        	cur = cur->next[chIdx];
        	}
        	ans.push_back(' ');
        }
        ans.pop_back();//删除空格
        return ans;
    }
};
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. Trie解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档