Created
April 26, 2013 04:14
-
-
Save rohith2506/5465000 to your computer and use it in GitHub Desktop.
palindrome partitioning (leetcode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
"This is palindrome partitioning" | |
@author : rohit | |
*/ | |
#include <iostream> | |
#include <stdio.h> | |
#include <cmath> | |
#include <vector> | |
#include <set> | |
#include <algorithm> | |
using namespace std; | |
vector<vector<string> > vec; | |
vector<int> idx; | |
bool ispalindrome(string &s,int i,int j){ | |
while(i<j){ | |
if(s[i++]!=s[j--]) | |
return false; | |
} | |
return true; | |
} | |
void backtrack(string s,int i){ | |
if(i==s.length()){ | |
vector<string> v; | |
int len = idx.size(); | |
for(int k=0;k<len-1;k++) | |
v.push_back(s.substr(idx[k]+1,idx[k+1]-idx[k])); | |
vec.push_back(v); | |
return ; | |
} | |
else{ | |
for(int j=i;j<s.length();j++){ | |
if(ispalindrome(s,i,j)){ | |
idx.push_back(j); | |
backtrack(s,j+1); | |
idx.pop_back(); | |
} | |
} | |
} | |
} | |
void func(string s){ | |
if(s.length() == 0) | |
return ; | |
idx.push_back(-1); | |
backtrack(s,0); | |
} | |
int main(){ | |
string str; | |
cin>>str; | |
func(str); | |
for(int i=0;i<vec.size();i++){ | |
for(int j=0;j<vec[i].size();j++){ | |
cout<<vec[i][j]<<" "; | |
} | |
cout<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment