DEV Community

Guhan M
Guhan M

Posted on

Creating Strings | CSES - Algoritmit ongelmanratkaisussa

Creating Strings - Problem link

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <climits>
#include <cinttypes>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef long int longi;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template <class T> int sz(const T &x) { return x.size(); }
const int INF = 2147483647;
#define rep(i,a,b) for (__typeof(a) i=(a); i<(b); ++i)
#define iter(it,c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)

set<string> s;

void permute(int i, string str) {
    if (i == sz(str)) {
        s.insert(str);
        return;
    }
    for (int j = i; j < sz(str); j++) {
        swap(str[i], str[j]);
        permute(i + 1, str);
        swap(str[i], str[j]);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string str;
    cin >> str;

    permute(0, str);
    cout << sz(s) << endl;
    iter(it, s) cout << *it << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)