首页 > 其他 > 详细

Word Break

时间:2014-10-04 10:48:06      阅读:286      评论:0      收藏:0      [点我收藏+]

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".

 

算法:在网上查到的,动态规划算法

 1 class Solution {
 2 public:
 3     bool wordBreak(string s, unordered_set<string> &dict) {
 4         int len=s.size();
 5         bool *k=new bool[s.size()];
 6         memset(k,false,s.size());
 7         for(int i=0;i<len;i++)
 8         {
 9             k[i]=dict.find(s.substr(0,i+1))!=dict.end()?true:false;
10             if(k[i])  continue;
11             for(int j=0;j<i;j++)
12             {
13                 if(k[j])
14                 {
15                     k[i]=dict.find(s.substr(j+1,i-j))!=dict.end()?true:false;
16                     if(k[i])  break;
17                 }
18             }
19         }
20         return k[len-1];
21     }
22 };

 

Word Break

原文:http://www.cnblogs.com/sqxw/p/4005675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!