zoukankan      html  css  js  c++  java
  • [Leetcode] Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome.

    Return all possible palindrome partitioning of s.

    For example, given s = "aab",
    Return

      [
        ["aa","b"],
        ["a","a","b"]
      ]

    像这样列举所有结果的只能DFS了。不过为什么我老是忘记写++low跟--high呢,都死循环了自己还不知道!

     1 class Solution {
     2 public:
     3     bool isPail(string &s) {
     4         int low = 0, high = s.length() - 1;
     5         while (low < high) {
     6             if (s[low] != s[high]) {
     7                 return false;
     8             }
     9             ++low;
    10             --high;
    11         }
    12         return true;
    13     }
    14     
    15     void findNext(vector<vector<string> > &res, string &s, vector<string> part, int idx) {
    16         if (idx == s.length()) {
    17             res.push_back(part);
    18             return;
    19         }
    20         string substr;
    21         for (int len = s.length() - idx; len > 0; --len) {
    22             substr = s.substr(idx, len);
    23             if (isPail(substr)) {
    24                 part.push_back(substr);
    25                 findNext(res, s, part, idx + len);
    26                 part.pop_back();
    27             }
    28         }
    29     }
    30     
    31     vector<vector<string>> partition(string s) {
    32         vector<vector<string> > res;
    33         vector<string> part;
    34         findNext(res, s, part, 0);
    35         return res;
    36     }
    37 };
  • 相关阅读:
    【链表】Bzoj1098[POI2007]办公楼biu
    【构造】Bzoj1432[ZJOI2009]Function
    【Dp】Bzoj1296 [SCOI2009] 粉刷匠
    【二分贪心】Bzoj3969 [WF2013] Low Power
    【递推】Bzoj3612[Heoi2014]平衡
    HDU-3718 Similarity
    HDU-3435 A new Graph Game
    HDU-3488 Tour
    HDU-1853 Cyclic Tour
    HDU-1533 Going Home
  • 原文地址:https://www.cnblogs.com/easonliu/p/3657733.html
Copyright © 2011-2022 走看看