zoukankan      html  css  js  c++  java
  • LeetCode:Word Break II(DP)

    题目地址:请戳我

    这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解。但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体可以对比两段代码),如果不去掉则会漏掉一些解(前一道题加约束条件是为了更快的判断是字符串是够能被分词,这里是为了找出所有分词的情况)

    代码如下:

     1 class Solution {
     2 public:
     3     vector<string> wordBreak(string s, unordered_set<string> &dict)
     4     {
     5         // Note: The Solution object is instantiated only once and is reused by each test case.
     6         vector<string> result;
     7         if(dict.empty())
     8             return result;
     9         const int len = s.size();
    10         bool canBreak[len]; //canBreak[i] = true 表示s[0~i]是否能break
    11         memset(canBreak, 0, sizeof(bool)*len);
    12         bool **pre = new bool *[len];//如果s[k..i]是字典中的单词,则pre[i][k]=true
    13         for(int i = 0; i < len; i++)
    14         {
    15             pre[i] = new bool[len];
    16             memset(pre[i], 0 , sizeof(bool)*len);
    17         }
    18 
    19         for(int i = 1; i <= len; i++)
    20         {
    21             if(dictContain(dict, s.substr(0, i)))
    22             {
    23                 canBreak[i-1] = true;
    24                 pre[i-1][0] = true;
    25             }
    26             if(canBreak[i-1] == true)
    27             {
    28                 for(int j = 1; j <= len - i; j++)
    29                 {
    30                     if(dictContain(dict,s.substr(i, j)))
    31                     {
    32                         canBreak[j+i-1] = true;
    33                         pre[j+i-1][i] = true;
    34                     }
    35                 }
    36             }
    37         }
    38         //return false;
    39         vector<int> insertPos;
    40         getResult(s, pre, len, len-1, insertPos, result);
    41         return result;
    42     }
    43 
    44     bool dictContain(unordered_set<string> &dict, string s)
    45     {
    46         unordered_set<string>::iterator ite = dict.find(s);
    47         if(ite != dict.end())
    48             return true;
    49         else return false;
    50     }
    51 
    52     //在字符串的某些位置插入空格,返回新字符串
    53     string insertBlank(string s,vector<int>pos)
    54     {
    55         string result = "";
    56         int base = 0;
    57         for(int i = pos.size()-1; i>=0; i--)
    58         {
    59             if(pos[i] == 0)continue;//开始位置不用插入空格
    60             result += (s.substr(base, pos[i]-base) + " ");
    61             base = pos[i];
    62         }
    63         result += s.substr(base, s.length()-base);
    64         return result;
    65     }
    66 
    67     //从前驱路径中构造结果
    68     void getResult(string s, bool **pre, int len, int currentPos,
    69                    vector<int>insertPos,
    70                    vector<string> &result)
    71     {
    72         if(currentPos == -1)
    73         {
    74             result.push_back(insertBlank(s,insertPos));
    75             //cout<<insertBlank(s,insertPos)<<endl;
    76             return;
    77         }
    78         for(int i = 0; i < len; i++)
    79         {
    80             if(pre[currentPos][i] == true)
    81             {
    82                 insertPos.push_back(i);
    83                 getResult(s, pre, len, i-1, insertPos, result);
    84                 insertPos.pop_back();
    85             }
    86         }
    87     }
    88 };

     【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3385644.html

  • 相关阅读:
    DFS,BFS算法
    浙江理工大学7月月赛
    矩阵快速幂
    数塔
    Bone Collector
    畅通工程
    敌兵布阵
    Tempter of the Bone
    Elevator
    Fibonacci Again
  • 原文地址:https://www.cnblogs.com/TenosDoIt/p/3385644.html
Copyright © 2011-2022 走看看