Palindrome Partitioning系列

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   io   for   資料   

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

 方法一:暴力破解,直接深度搜尋字串,直到找到所有解為止。假設字串為S=a0a1a2a3a4a5...an,那麼從下標0開始往後找迴文字串,假設下標為i時有S[0...i]是迴文,則將原問題化為子問題,即在S[i+1...n]中繼續從頭往後尋找迴文字串,詳細請看代碼:

 1 class Solution { 2 public: 3     vector<vector<string>> partition(string s) { 4         ans.clear(); 5         vector<string> vstrs; 6         dfs(vstrs, s); 7         return ans; 8     } 9     10 private:11     void dfs(vector<string>& vstrs, string s) {     //深度搜尋12         if( s.empty() ) {   //如果s為空白串,則說明前面的切割出的字串均為迴文13             ans.push_back(vstrs);14             return ;15         }16         for(int i=1; i<=s.length(); ++i) {  //依次從前往後開始切割17             string tstr = s.substr(0, i);18             if( ispalindrome(tstr) ) {  //如果被切割出的字串是迴文19                 vstrs.push_back(tstr);  //存放結果20                 dfs(vstrs, s.substr(i, s.length()-i));  //繼續深搜未被切割的後面的字串21                 vstrs.pop_back();   //pop結果22             }23         }24     }25     26     //判斷是否是迴文27     bool ispalindrome(const string& s) {28         for(int i=0, j=s.length()-1; i<j; ++i,--j)29             if( s[i] != s[j] ) return false;30         return true;31     }32     33 private:34     vector< vector<string> > ans;   //儲存切割後的結果35 };

方法二:轉為dp問題,另dp[i][j] 以 i 為起點,長度為 j 的的字串,則有

當 s[i] == s[i+j-1] 且 dp[i+1][j-2] == true 時 dp[i][j] = true;否則為false

當 j = 1 時 dp[i][j] = true;

最後通過dp二維數組,依然深度搜尋字串,得出最後的結果,代碼如下:

 

 1 class Solution { 2 public: 3     vector<vector<string>> partition(string s) { 4         if( s.empty() ) 5             return vector< vector<string> >(); 6         vector< vector<bool> > dp(s.length(), vector<bool>(s.length()+1, false));   //行代表子串起始座標,列代表字串長度 7         for(int j=0; j<2; ++j)  //長度為0,1都認為是迴文 8             for(int i=0; i<s.length(); ++i) 9                 dp[i][j] = true;10         for(int j=2; j<=s.length(); ++j)    //從長度為2開始計算11             for(int i=0; i<=s.length()-j; ++i)12                 dp[i][j] = dp[i+1][j-2] && (s[i] == s[i+j-1]);13         ans.clear();14         vector<string> vstrs;15         dfs(dp, vstrs, s, 0, s.length());   //深搜dp,找出滿足條件的結果16         return ans;17     }18     19     //k為子串起始下標20     void dfs(vector<vector<bool> >& dp, vector<string>& vstrs, string& s, int k, int n) {21         if( k >= n ) {22             ans.push_back(vstrs);23             return ;24         }25         int len = n-k;  //子串[k...n-1]的長度26         for(int i=1; i<=len; ++i) { //長度為1開始遍曆27             if( dp[k][i] ) {    //如果字串s[k...k+i-1]是迴文28                 vstrs.push_back(s.substr(k,i));29                 dfs(dp, vstrs, s, k+i, n);30                 vstrs.pop_back();31             }32         }33     }34     35 private:36     vector< vector<string> > ans;   //儲存切割後的結果37 };
Palindrome Partitioning II

 

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

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

 

Palindrome Partitioning系列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.