4495. Print permutations
Description
A permutation is a possible ordering of a set. For example, the permutations of a set {A,B,C} include ABC, ACB, BAC, BCA, CAB, CBA.
The number of permutations of a set of N elements is N!. The example set above has 3 elements and it has a number of 3!=6 permutations.
Given a line with N different upper letters, please print N! lines containing all different permutations of the N letters. These N! lines are printed in dictionary order.
Input
A line with N (1<=N<=8) different upper letters given in dictionary order.
Output
N! lines containing all different permutations of the N letters. These N! lines are printed in dictionary order.
Sample Input
ABCD
Sample Output
ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC BDCA CABD CADB CBAD CBDA CDAB CDBA DABC DACB DBAC DBCA DCAB DCBA
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string a;
getline(cin,a);
int len=a.length();
int b[8];
for(int i=0;i<len;i++)
b[i]=a[i]-'0';
do
{
for(int i = 0; i < len; i++)
{
cout << char(b[i]+'0');
}
cout <<endl;
} while (next_permutation(b, b + len));
return 0;
}