Word Break
来源:互联网 发布:apache ab测试 linux 编辑:程序博客网 时间:2024/05/16 09:56
原题:
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
解:
class Solution {public: bool wordBreak(string s, unordered_set<string>& wordDict) { if(s.size() && wordDict.size()==0){ return false; } int len=s.size(); bool * dp=new bool[len+1]; for(int i=0; i<len+1; i++){ dp[i]=false; } int maxLen=0; int minLen=(1<<31-1); for(auto it=wordDict.begin(); it!=wordDict.end(); it++){ if(maxLen<it->size()) maxLen=it->size(); if(minLen>it->size()) minLen=it->size(); } dp[0]=true; for(int i=1; i<=len; i++){ for(int j=minLen; j<=i; j++){//j表示长度,从而一次计算[i-1,i),[i-2,i), [i-3,i)的字符串,看能否形成一个切分 if(wordDict.find(s.substr(i-j, j))!=wordDict.end()) dp[i]=dp[i-j];//终点在i-1处可以到达 if(dp[i]) break; } } bool ret=dp[len]; delete [] dp; return ret; }};
0 0
- Word-break:break-word
- Word Break && Word Break ||
- word-break
- Word Break
- Word Break
- Word Break
- word break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- 对高价流量说不 高性价比流量王——亿富流量银行
- Mono for Unreal Engine发布,C#进入虚幻引擎(Unreal Engine)
- PAT 1030. Travel Plan (30)
- Memory barrier(内存屏障)
- 1009:FatMouse' Trade
- Word Break
- 3件Unreal Engine 3不得不说的故事
- 同步和条件变量
- poj 2631 Roads in the North 【树的直径裸题】
- IBInspectable 和IB_DESIGNABLE动态刷新IB
- 关于二分查找, 关注细节
- MySQL 同步机制
- 如何建立压力计算模型
- 1003 快速排序