String Permutation according size using STL ++ and print lexicographic order.

/* String permutation according the permuted string size  using C++ STL* and display  lexicographic order , using next_permutation function*/

#include<iostream>
#include <set>
#include<map>
#include <string>
#include<string.h>
#include<algorithm>
#define MAX_SIZE 100
using namespace std;

int main ()
{
string str,temp;
map<int,set<string>> mymap;
char ch[MAX_SIZE];
int i,j,len,length;
cin>>str;
len=str.size();
strcpy(ch,str.c_str());
sort(ch,ch+len);
str=ch;
for(i=0;i<len;i++)
{
for(j=1;j<=len-i;j++)
{
temp=str.substr(i,j);
strcpy(ch,temp.c_str());
length=temp.size();
do
{
mymap[length].insert(ch);
}while(next_permutation(ch,ch+j));
}
}
map<int,set<string>>::iterator mit;
set<string>::iterator sit;
for(mit=mymap.begin();mit!=mymap.end();mit++)
for(sit=mit->second.begin();sit!=mit->second.end();sit++)
cout<<*sit<<endl;
    return 0;
}

Input:

        abc

Output:


a
b
c
ab
ba
bc
cb
abc
acb
bac
bca
cab
cba

Input:

        dbba

Output:


a
b
d
ab
ba
bb
bd
db
abb
bab
bba
bbd
bdb
dbb
abbd
abdb
adbb
babd
badb
bbad
bbda
bdab
bdba
dabb
dbab
dbba



Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System