LeetCode OJ——Word Break
http://oj.leetcode.com/problems/word-break/
动态规划题目,重点是建立出模型来:
fun(start,end) = fun(start,i)*fun(i+1,end);
二维动态数组的申请:
int len = s.length();
int **flag = new int *[len];
for(int i = 0;i<len;i++)
flag[i] = new int [len];
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std; class Solution {
public:
bool fun(string s,unordered_set<string> &dict,int start,int end,int **flag)
{
if(flag[start][end]==)
return true;
if(flag[start][end]==-)
return false;
string subString = s.substr(start,end-start+);
if(dict.find(subString)!=dict.end())
return true;
for(int i = start;i<end;i++)
if(fun(s,dict,start,i,flag)*fun(s,dict,i+,end,flag))
{
flag[start][end] = ;
return ;
}
flag[start][end] = -;
return false;
}
bool wordBreak(string s, unordered_set<string> &dict) {
int len = s.length();
int **flag = new int *[len];
for(int i = ;i<len;i++)
flag[i] = new int [len]; return fun(s,dict,,len-,flag);
}
}; int main()
{
class Solution mys;
string s = "leetcode";
unordered_set<string> dict;
dict.insert("le");
//dict.insert("et");
dict.insert("code");
cout<<mys.wordBreak(s,dict);
return ;
}
LeetCode OJ——Word Break的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- [Leetcode Week9]Word Break
Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...
- 【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 ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- LeetCode OJ——Word Ladder2
http://oj.leetcode.com/problems/word-ladder-ii/ class Solution { public: vector<vector<string& ...
随机推荐
- MySQL自学笔记_聚集函数
1. 使用场景 很多时候我们需要查找数据库中符合特定条件的数据的计数.最大值.最小值.平均值等一个数字,并需要要导出所有相关数据明细.此时就需要用到聚集函数. 而返回所有数据明细会占用数据库资源和网络 ...
- LeetCode(165) Compare Version Numbers
题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- FSMC原理通俗解释
所以不用GPIO口直接驱动液晶,是因为这种方法速度太慢,而FSMC是用来外接各种存储芯片的,所以其数据通信速度是比普通GPIO口要快得多的.TFT-LCD 驱动芯片的读写时序和SRAM的差不多,所以就 ...
- poj-2533 longest ordered subsequence(动态规划)
Time limit2000 ms Memory limit65536 kB A numeric sequence of ai is ordered if a1 < a2 < ... &l ...
- 动态规划:Codeforces Round #427 (Div. 2) C Star sky
C. Star sky time limit per test2 seconds memory limit per test256 megabytes inputstandard input outp ...
- poj 3669 火星撞地球问题 bfs算法
题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间 思路:bfs+预处理 这题的数据量比较大,所以需要进行预处理 对每个位置设上时间(被撞的最早时间) 未被撞的设为-1 for (int j = ...
- 百度地图的API接口----多地址查询和经纬度
最近看了百度地图的API的接口,正想自己做点小东西,主要是多地址查询和经纬度坐标跟踪, 下面的代码直接另存为html就可以了,目前测试Chrome和360浏览器可以正常使用. <!DOCTYPE ...
- loj2016 「SCOI2016」美味
trie 树思想运用到主席树上orz #include <iostream> #include <cstdio> using namespace std; int n, m, ...
- 树状数组 - BZOJ 1103 [POI2007]大都市
bzoj 1103 [POI2007]大都市 描述 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员 Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. ...