Skip to content

Instantly share code, notes, and snippets.

@recursivecurry
Created September 19, 2016 05:15
Show Gist options
  • Save recursivecurry/418c1b2dd55271027fb4b3b2f9173f7a to your computer and use it in GitHub Desktop.
Save recursivecurry/418c1b2dd55271027fb4b3b2f9173f7a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <set>
#include <vector>
using namespace std;
int main() {
int n = 0;
string nothing;
set<vector<int>> current;
set<vector<int>> next;
vector<int> ss;
cin >> n;
ss.reserve(n);
for (int i=0; i<n; i++) {
int v;
cin >> v;
ss.push_back(v);
}
current.insert(ss);
for (int k=1; k<n; k++) {
for (auto s : current) {
for (int from=0; from<s.size()-1; from++) {
for (int to=from+1; to<s.size(); to++) {
char from_val = s[from];
char to_val = s[to];
s[to] = from_val;
s[from] = to_val;
next.insert(s);
s[to] = to_val;
s[from] = from_val;
}
}
}
std::cout << next.size() << " ";
current.clear();
current.swap(next);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment