Skip to content

Instantly share code, notes, and snippets.

@krisys
Created April 28, 2011 17:50
Show Gist options
  • Save krisys/946862 to your computer and use it in GitHub Desktop.
Save krisys/946862 to your computer and use it in GitHub Desktop.
Permutation (Minimal Change)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n;
char c;
vector < string > next, current;
string set = "abc", start = "a", temp = "";
current.push_back(start);
while( current[0].length() < set.length() ){
for(int i = 0; i < current.size() ; i++){
temp = current[i];
temp = set[ temp.length() ] + temp;
next.push_back(temp);
for(int j = 1 ; j < temp.length() ; j++){
c = temp[j];
temp[j] = temp[j-1];
temp[j-1] =c;
next.push_back(temp);
}
}
current = next;
next.clear();
}
sort(current.begin(), current.end());
for(int i = 0 ; i < current.size() ; i++)
cout << current[i] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment