Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

这道题是 Word Break的一个拓展

问题:根据给的单词字典,求一个字符串所有可能的有效分割。

有了 Word Break 的经验,这道题做起来也顺畅了许多。

假设将字符串 s 分割为两段,[0,i-1], [i, n-1],如果[0, i-1] 为有效单词,[i, n-1]为有效单词集合,那么 s 就是一个有效字符串。将 [0, i-1] 依次和 [i, n-1] 所有可能分别匹配,则得到 s 以 i 为分割点得全部有效分割。

将 i 从 1 到 n-1 遍历一次,则求得 s 的全部分割的可能。

和 Word Break 一样,需要记录已计算的结果,提高效率。利用 map<int, vector<string>> idx_words[k] 来记录 [k, n-1] 子串的全部有效分割。

    map<int, vector<string>> idx_words;

    vector<string> match(string s, unordered_set<string>& wordDict, int startIdx){
vector<string> res; for (int i = ; i < s.size(); i++) {
string leftS = s.substr(, i); unordered_set<string>::iterator us_iter = wordDict.find(leftS); if (us_iter != wordDict.end()) { int rightRLIdx = i + startIdx; vector<string> rightV;
if (idx_words.find(rightRLIdx) != idx_words.end()){
rightV = idx_words[rightRLIdx];
}else{ string rightS = s.substr(i, s.size() - i); rightV = match(rightS, wordDict, rightRLIdx);
idx_words[rightRLIdx] = rightV;
} for (int ii = ; ii < rightV.size(); ii++) {
string tmpS = leftS + " " + rightV[ii]; res.push_back(tmpS);
}
}
} if (wordDict.find(s) != wordDict.end()) {
res.push_back(s);
} return res;
} vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<string> res; res = match(s, wordDict, ); return res;
}

[LeetCode] Word Break II 解题思路的更多相关文章

  1. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  2. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  3. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  4. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  5. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  6. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  7. [Leetcode] word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  9. LeetCode: Word Break II [140]

    [题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...

随机推荐

  1. Android(java)学习笔记230:服务(service)之绑定服务的细节

    绑定服务的细节 1. 如果onbind方法返回值是null,onServiceConnect方法就不会被调用: 2. 绑定的服务,在系统设置界面,正在运行条目是看不到的: 3. 绑定的服务,不求同时生 ...

  2. EventBus分析

    1. 功能介绍 1.1 EventBus EventBus 是一个 Android 事件发布/订阅框架,通过解耦发布者和订阅者简化 Android 事件传递,这里的事件可以理解为消息,本文中统一称为事 ...

  3. HDU 4607 Park Visit(树的直径)

    题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...

  4. codevs1226倒水问题(Bfs)

    /* 首先建立模型 可以看成是三个水杯 第三个无穷大 (这里看成是201足够了) 最少步数 想到Bfs 维护队列里的状态:要有个步数 还要有v :此时刻三个杯子有多少水 然后倒水:因为没有刻度 所以有 ...

  5. Python的基本配置

    Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结 ...

  6. Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题

    最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.c ...

  7. 关于T-SQL重编译那点事,内联函数和表值函数在编译生成执行计划的区别

    本文出处:http://www.cnblogs.com/wy123/p/6266724.html 最近在学习 WITH RECOMPILE和OPTION(RECOMPILE)在重编译上的区别的时候,无 ...

  8. JavaScript--声明提前

    声明提前(hoist): 在正式执行程序前,都会将所有var声明的变量和function声明的函数提前到*当前作用域*的顶部集中创建. 但是,赋值留在原地. console.log(a);//unde ...

  9. qt二维码示例

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://blog.csdn.net/hiwubihe/article/details/38679621,qq:1269122125. 移动终 ...

  10. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...