File size: 715 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
  >>~~ UVa Online Judge ACM Problem Solution ~~<<

  ID: 10063
  Name: Knuth's Permutation
  Problem: https://onlinejudge.org/external/100/10063.pdf
  Language: C++

  Author: Arash Shakery
  Email: arash.shakery@gmail.com
*/

#include <bits/stdc++.h>
using namespace std;


char S[17], P[17], n;
void dfs(int idx) {
    if (idx == n) {
        puts(P);
        return;
    }

    for (int i=idx; i>0; --i)
        P[i] = P[i-1];

    for (int i=0; i<=idx; ++i) {
        P[i] = S[idx];
        dfs(idx + 1);
        P[i] = P[i+1];
    }
}

int main() {
    bool frst = 1;
    while (cin.getline(S, 17)) {
        if (frst) frst = 0;
        else puts("");

        P[ n=strlen(S) ] = 0;
        dfs(0);
    }
}