C++实现LeetCode(648.替换单词)

[LeetCode] 648.Replace Words 替换单词

In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another word another.

Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.

You need to output the sentence after the replacement.

Example 1:

Input: dict = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"

Note:

  1. The input will only have lower-case letters.
  2. 1 <= dict words number <= 1000
  3. 1 <= sentence words number <= 1000
  4. 1 <= root length <= 100
  5. 1 <= sentence words length <= 1000

这道题给了我们一个前缀字典,又给了一个句子,让我们将句子中较长的单词换成其前缀(如果在前缀字典中存在的话)。我们对于句子中的一个长单词如何找前缀呢,是不是可以根据第一个字母来快速定位呢,比如cattle这个单词的首字母是c,那么我们在前缀字典中找所有开头是c的前缀,为了方便查找,我们将首字母相同的前缀都放到同一个数组中,总共需要26个数组,所以我们可以定义一个二维数组来装这些前缀。还有,我们希望短前缀在长前缀的前面,因为题目中要求用最短的前缀来替换单词,所以我们可以先按单词的长度来给所有的前缀排序,然后再依次加入对应的数组中,这样就可以保证短的前缀在前面。

下面我们就要来遍历句子中的每一个单词了,由于C++中没有split函数,所以我们就采用字符串流来提取每一个单词,对于遍历到的单词,我们根据其首字母查找对应数组中所有以该首字母开始的前缀,然后直接用substr函数来提取单词中和前缀长度相同的子字符串来跟前缀比较,如果二者相等,说明可以用前缀来替换单词,然后break掉for循环。别忘了单词之前还要加上空格,参见代码如下:

解法一:

class Solution {
public:
    string replaceWords(vector<string>& dict, string sentence) {
        string res = "", t = "";
        vector<vector<string>> v(26);
        istringstream is(sentence);
        sort(dict.begin(), dict.end(), [](string &a, string &b) {return a.size() < b.size();});
        for (string word : dict) {
            v[word[0] - 'a'].push_back(word);
        }
        while (is >> t) {
            for (string word : v[t[0] - 'a']) {
                if (t.substr(0, word.size()) == word) {
                    t = word;
                    break;
                }
            }
            res += t + " ";
        }
        res.pop_back();
        return res;
    }
};

你以为想出了上面的解法,这道题就算做完了?? Naive! ! ! 这道题最好的解法其实是用前缀树(Trie / Prefix Tree)来做,关于前缀树使用之前有一道很好的入门题Implement Trie (Prefix Tree)。了解了前缀树的原理机制,那么我们就可以发现这道题其实很适合前缀树的特点。我们要做的就是把所有的前缀都放到前缀树里面,而且在前缀的最后一个结点的地方将标示isWord设为true,表示从根节点到当前结点是一个前缀,然后我们在遍历单词中的每一个字母,我们都在前缀树查找,如果当前字母对应的结点的表示isWord是true,我们就返回这个前缀,如果当前字母对应的结点在前缀树中不存在,我们就返回原单词,这样就能完美的解决问题了。所以啊,以后遇到了有关前缀或者类似的问题,一定不要忘了前缀树这个神器哟~

解法二:

class Solution {
public:
    class TrieNode {
    public:
        bool isWord;
        TrieNode *child[26];
        TrieNode(): isWord(false) {
            for (auto &a : child) a = NULL;
        }
    };

    string replaceWords(vector<string>& dict, string sentence) {
        string res = "", t = "";
        istringstream is(sentence);
        TrieNode *root = new TrieNode();
        for (string word : dict) {
            insert(root, word);
        }
        while (is >> t) {
            if (!res.empty()) res += " ";
            res += findPrefix(root, t);
        }
        return res;
    }

    void insert(TrieNode* node, string word) {
        for (char c : word) {
            if (!node->child[c - 'a']) node->child[c - 'a'] = new TrieNode();
            node = node->child[c - 'a'];
        }
        node->isWord = true;
    }

    string findPrefix(TrieNode* node, string word) {
        string cur = "";
        for (char c : word) {
            if (!node->child[c - 'a']) break;
            cur.push_back(c);
            node = node->child[c - 'a'];
            if (node->isWord) return cur;
        }
        return word;
    }
};

类似题目:

Implement Trie (Prefix Tree)

参考资料:

https://discuss.leetcode.com/topic/97203/trie-tree-concise-java-solution-easy-to-understand

到此这篇关于C++实现LeetCode(648.替换单词)的文章就介绍到这了,更多相关C++实现替换单词内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++实现LeetCode(202.快乐数)

    [LeetCode] 202.Happy Number 快乐数 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits,

  • C++实现LeetCode(642.设计搜索自动补全系统)

    [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统 Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#'). For each character they type except '#', you ne

  • C++实现LeetCode(203.移除链表元素)

    [LeetCode] 203.Remove Linked List Elements 移除链表元素 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 Credits

  • C++实现LeetCode(237.删除链表的节点)

    [LeetCode] 237.Delete Node in a Linked List 删除链表的节点 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with va

  • C++实现LeetCode(211.添加和查找单词-数据结构设计)

    [LeetCode] 211.Add and Search Word - Data structure design 添加和查找单词-数据结构设计 Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string c

  • C++实现LeetCode(201.数字范围位相与)

    [LeetCode] 201.Bitwise AND of Numbers Range 数字范围位相与 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits: Special t

  • C++实现LeetCode(208.实现字典树(前缀树))

    [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树) Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple");   // returns true trie.search("app&

  • C++实现LeetCode(207.课程清单)

    [LeetCode] 207. Course Schedule 课程清单 There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given

  • C++实现LeetCode(648.替换单词)

    [LeetCode] 648.Replace Words 替换单词 In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another w

  • 在VS2008中使用正则表达式进行查找和替换

    正则表达式是查找和替换文本模式的一种简洁而灵活的表示法. 在"查找和替换"窗口中执行"快速查找"."在文件中查找"."快速替换"或"在文件中替换"操作时,可以在该窗口的"查找内容"和"替换为"字段中使用一组专用的正则表达式. 若要启用正则表达式,请在"查找和替换"窗口中展开"查找选项",选择"使用",然后选择

  • 比较详细Python正则表达式操作指南(re使用)

    就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.使用这个小型语言,你可以为想要匹配的相应字符串集指定规则:该字符串集可能包含英文语句.e-mail地址.TeX命令或任何你想搞定的东西.然後你可以问诸如"这个字符串匹配该模式吗?"或"在这个字符串中是否有部分匹配该模式呢?".你也可以使用 RE 以各种方式来修改或分割字符串. 正则表达式模式被编译成一系列的字节码,然後由用 C

  • PHP游戏编程25个脚本代码

    清单 1.简单的掷骰器 许多游戏和游戏系统都需要骰子.让我们先从简单的部分入手:掷一个六面骰子.实际上,滚动一个六面骰子就是从 1 到 6 之间选择一个随机数字.在 PHP 中,这十分简单:echo rand(1,6);. 在许多情况下,这基本上很简单.但是在处理机率游戏时,我们需要一些更好的实现.PHP 提供了更好的随机数字生成器:mt_rand().在不深入研究两者差别的情况下,可以认为 mt_rand 是一个更快.更好的随机数字生成器:echo mt_rand(1,6);.如果把该随机数字

  • Python 正则表达式操作指南

    原文作者:A.M. Kuchling (amk@amk.ca) 授权许可:创作共享协议 翻译人员:FireHare 校对人员:Leal 适用版本:Python 1.5 及后续版本http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D%97#.E7.BC.96.E8.AF.91.E6.AD.A3.E5.88.99.E8.A1.A8.E

  • PHP 编写的 25个游戏脚本

    无论是一个人玩简单的使用纸和笔的游戏,还是同一群人玩复杂的桌面角色扮演游戏,或者任意类型的联机游戏,本系列都提供了适合您的内容."用 PHP 可以编写的 30 个游戏脚本" 系列中的每篇文章都将分别用不到 300 词的文字介绍 10 个脚本(3d10 表示 "掷三个 10 面的骰子"),这些介绍性文字甚至对于开发新手来说都十分简单,而且对于经验丰富的游戏玩家来说也十分有用.本系列的目的在于为您提供可以修改的内容来满足自身的需求,以便您可以在下一次游戏交流会上通过展示

  • PYTHON正则表达式 re模块使用说明

    首先,运行 Python 解释器,导入 re 模块并编译一个 RE: #!python Python 2.2.2 (#1, Feb 10 2003, 12:57:01) >>> import re >>> p = re.compile('[a-z]+') >>> p <_sre.SRE_Pattern object at 80c3c28> 现在,你可以试着用 RE 的 [a-z]+ 去匹配不同的字符串.一个空字符串将根本不能匹配,因为 +

  • Python IDLE入门简介

    IDLE是Python软件包自带的一个集成开发环境,初学者可以利用它方便地创建.运行.测试和调试Python程序. 参考: pip和pygal的安装实例教程 Python(一)运行环境搭建 一.IDLE的安装 实际上,IDLE是跟Python一起安装的,不过要确保安装时选中了"Tcl/Tk"组件,准确地说,应该是不要取消该组件,因为默认时该组件是处于选中状态的. 二.IDLE的启动 安装Python后,我们可以从"开始"菜单→"所有程序"→&qu

  • Java Idea TranslationPlugin翻译插件使用解析

    开发中,对于不经常使用英语的同学来说,对类,变量,方法想取一个合适的名字,此时发现自己的词汇早已还给老师 ,怎么办,这个插件能帮到你~ 一.安装 点击File-- Settings--Plugins设置界面,安装Translation插件. 二.使用 2.1 翻译单词 选中单词,右击菜单里面有"Translate". 2.2 替换单词 2.3 文本翻译 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们.

  • Pipes实现LeetCode(192.单词频率)

    [LeetCode] 192.Word Frequency 单词频率 Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume: words.txt contains only lowercase characters and space ' ' characters. Each word must consis

随机推荐