Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254196
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: C/C++

2014-12-05 18:00:24

Word Break

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

才发现我一直把题意理解错了,原来是源串分出来的词只要都在字典里就好了,比如leetleet就可以分成两个leet都在词典里,原来不是所有字典里的词都要出现。

那就容易些了,另dp[i]  表示源串的前i个字符可以满足分割,那么 dp[ j ] 满足分割的条件是存在k 使得 dp [k] && substr[k,j]在字典里。

这是O(n2)的算法,注意下标,因为很恶心。


  1. class Solution {  
  2. public:  
  3.     bool wordBreak(string s, unordered_set &dict) {  
  4.         // Note: The Solution object is instantiated only once and is reused by each test case.  
  5.         int n = (int)s.size();  
  6.         vector<bool> dp(n + 1, false);  
  7.         dp[0]=true;  
  8.         for(int i=1;i<=n;i++)  
  9.         {  
  10.             if(dp[i-1])  
  11.             {  
  12.                 int idx=i-1;  
  13.                 for(int j=idx;j
  14.                 {  
  15.                     string cur=s.substr(idx,j-idx+1);  
  16.                     if(dict.count(cur)>0)  
  17.                         dp[j+1]=true;  
  18.                 }  
  19.             }  
  20.         }  
  21.         return dp[n];  
  22.     }  
  23. };  


Word Break II

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


跟第一题一样,就是要返回所有可能的切分, 做切分反应是用回溯,但是不剪枝肯定要超时。

这里用了一个math[i][j] 来表示 i--j 这一段是否可以切分,然后在dfs的时候利用看最后剩余的子串能否切分来剪枝。


  1. class Solution {  
  2. public:  
  3.     vector wordBreak(string s, unordered_set &dict)   
  4.     {  
  5.         int n=s.length();  
  6.         vectorbool> > match(n+1,vector<bool>(n+1,false));  
  7.         for(int i=0;i<=n;i++)  
  8.             match[0][i]=true;  
  9.         for(int len=1;len<=n;len++)  
  10.         {  
  11.             for(int start=0;start+len<=n;start++)  
  12.             {  
  13.                 string tmp=s.substr(start,len);  
  14.                 if(dict.count(tmp)>0)  
  15.                     match[len][start]=true;  
  16.                 else  
  17.                 {  
  18.                     for(int left=1;left
  19.                     {  
  20.                         match[len][start]=match[left][start]&&match[len-left][start+left];  
  21.                         if(match[len][start])  
  22.                             break;  
  23.                     }  
  24.                 }  
  25.             }  
  26.         }  
  27.         if(match[n][0]==false)  
  28.             return vector();  
  29.         vector ans;  
  30.         vector had;  
  31.         dfs(s,0,match,had,ans,dict);  
  32.         return ans;  
  33.     }  
  34.     void dfs(string& s,int k,vectorbool> >& match,vector& had,vector& ans,unordered_set &dict)  
  35.     {  
  36.         int n=s.length();  
  37.         if(k>=n)  
  38.         {  
  39.             if(!had.empty())  
  40.             {  
  41.                 string ret;  
  42.                 for(int i=0;i
  43.                 {  
  44.                     ret.append(had[i]);  
  45.                     if(i!=had.size()-1)  
  46.                         ret.push_back(' ');  
  47.                 }  
  48.                 ans.push_back(ret);  
  49.                 return;  
  50.             }  
  51.         }  
  52.         for(int len=1;k+len<=n;len++)  
  53.         {  
  54.             string tmp=s.substr(k,len);  
  55.             if(dict.count(tmp)>0 && match[n-k-len][k+len])  
  56.             {  
  57.                 had.push_back(tmp);  
  58.                 dfs(s,k+len,match,had,ans,dict);  
  59.                 had.pop_back();  
  60.             }  
  61.         }  
  62.     }     
  63. };  
阅读(919) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~