动态规划之字符串分词

(一)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".

动态规划:划分为子问题
1. dp[j]:表示0-j可分,s[i][j]:表示i,j字符串在dict中
2. dp[j] = true,当dp[i]&s[i][j]

    bool wordBreak(string s, unordered_set<string> &dict) {
        vector<bool> dp(s.size()+1,false);
        dp[0] = true;
        for(int i = 0;i < s.size();i++)
           for(int j = i+1;dp[i]&&j<=s.size();j++)
                   if(dict.find(s.substr(i,j-i))!=dict.end()) dp[j] = true;
        return dp[s.size()];
    }

(二)work-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"].

动态规划:后面的运算用到前面的结果,把一个问题分解成多个子问题
1. dp[j]:表示0-j可分,s[i][j]:标识i-j字符串在dict中
2. dp[j] = dp[i],s[i][j]为真,0<= i < j
3. 重复2步骤,直到s[0][i]为假且s[0][j]不可分,或s[0][j]为真

vector<string> v;
 vector<bool> *d;
 vector<string> wordBreak(string s, unordered_set<string> &dict) {
       d = new vector<bool>[s.length()];
       for(int i = 0;i < s.size();i++)
           for(int j = 0;j <s.size();j++)
               d[i].push_back(false);

       for(int i = 0;i<s.size();i++){
           for(int j = i;j<s.size();j++){
               if( dict.find(s.substr(i,j-i+1)) != dict.end()) d[i][j] = true;
           }
       }
      string t;
      wordBreakHelp(s.size()-1,s,t);
      return v;
 }
  void wordBreakHelp(int k,string s,string t){
      string t0;
      for(int i = k;i>=0;i--){
          t0 = t;
          if(d[i][k]){
              if(i != 0){
                t0 = " " + s.substr(i,k-i+1) + t0;
                wordBreakHelp(i-1,s,t0);  
              } 
              else {
                t0 = s.substr(i,k-i+1) + t0;
                v.push_back(t0);
              }
          }
      }
  }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hujinglovekmg/article/details/78451611
文章标签: 动态规划
个人分类: c++算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭