Palindrome Partitioning, palindrome
/* 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", "", "B"] */# include <vector> # include <string> using namespace std; # if 0 // wrongclass Solution {public: vector <string> partition (string s) {int len = s. size (); vector <string> res; auto s_iter = s. begin (); For (int I = 0; I <len; I ++) {vector <string> tmp; for (int j = I; j <len; j ++) {// string s (s_iter + I, s_iter + j + 1); string str = s. substr (I, j + 1); if (is_palindrome (s, I, j) {tmp. push_back (s) ;}} res. push_back (tmp);} return res;} private: bool is_palindrome (string & s, int head_index, int tail_index) {while (head_index <tail_index) {if (s [head_index]! = S [tail_index]) return false; head_index ++; tail_index --;} return true ;};# elif 1 // LeetCode, palindrome Partitioning // time complexity O (2 ^ n), space complexity O (n) class Solution {public: vector <string> partition (string s) {vector <string> result; vector <string> output; // a partition scheme DFS (s, 0, output, result); return result ;} // search for the partition scheme void DFS (string & s, int start, vector <string> & ou that must start with s [start] Tput, vector <string> & result) {if (start = s. size () {result. push_back (output); return;} for (int I = start; I <s. size (); I ++) {if (isPalindrome (s, start, I) {output. push_back (s. substr (start, I-start + 1); DFS (s, I + 1, output, result); // continue to cut down the output. pop_back (); // revoke the previous push_back cut} bool isPalindrome (string & s, int start, int end) {while (start <end) {if (s [start]! = S [end]) return false; start ++; end --;} return true ;};# endifvoid test0 () {string s = "aab"; Solution ss; auto res = ss. partition (s); int we ;}int main () {test0 (); system ("pause"); return 0 ;}
Method 2: Motion Gauge
To solve this problem using dynamic planning, the key is to construct a solution space and determine whether any substring S (I, j) of S is symmetric. The criteria are as follows:
1. if I = j, S (I, j) is symmetric;
2. If j-I = 1 & S [I] = S [j], S (I, j) is symmetric;
3. If j-I> 1 & S [I] = S [j] & S (I + 1, j-1) is symmetric, S (I, j) is also symmetric.
Code: (f Initialization is true)
Equivalent to (better understanding and recommendation below)
// Because this topic requires various division of the entire string, it is best to obtain a table from any I to j that is a back-to-text. // Therefore, first, call setIsPalin to obtain the table // tmp. push_back (s. substr (start, end-start + 1) the sentence is placed in a good position. If it is placed in a loop, // push it and pop it out, too troublesome class Solution {vector <string> result; public: void setIsPalin (string s, vector <bool> & f, int N) {for (int I = N-2; I> = 0; I --) // must be cyclically from N-2 to 0 for (int j = I + 1; j <N; j ++) f [I] [j] = s [I] = s [j] & f [I + 1] [J-1];} void part (int start, int end, vector <string> tmp, string & s, vector <bool> & isPalin, int N) {tmp. push_back (s. substr (start, end-start + 1); // push is good here, because it is a copy, so it does not affect the original value if (end = N-1) {// If the string has been traversed, the result is recorded. push_back (tmp); return ;}for (int I = end + 1; I <N; I ++) {if (isPalin [end + 1] [I]) {part (end + 1, I, tmp, s, isPalin) ;}}} vector <string> partition (string s) {result. clear (); int N = s. size (); vector <bool> isPalin (N, vector <bool> (N, true); setIsPalin (s, isPalin, N ); vector <string> tmp; for (int I = 0; I <N; I ++) {if (isPalin [0] [I]) {// If the string is from 0 to I, it determines whether the part can be divided into the rest strings (0, I, tmp, s, isPalin, N );}} return result ;}};