String Permutation Using STL C++ and Print Lexicographic order


/* String Permutation Using STL C++ and Print Lexicographic order
    using next_permutation function.
*/
#include<iostream>
#include <set>
#include <string>
#include<string.h>
#include<algorithm>
#define MAX_SIZE 100
using namespace std;

int main ()
{
    string str,temp;
    set<string> myset;
    char ch[MAX_SIZE];
    int i,j,len;
    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());
            do
            {
                myset.insert(ch);
            }while(next_permutation(ch,ch+j));
        }
    }
    set<string>::iterator it;
    for(it=myset.begin();it!=myset.end();it++)
        cout<<*it<<endl;
    return 0;
}

Input: 
           bacda
Output:
  a
aa
aab
aabc
aabcd
aabdc
aacb
aacbd
aacdb
aadbc
aadcb
ab
aba
abac
abacd
abadc
abc
abca
abcad
abcd
abcda
abdac
abdc
abdca
acab
acabd
acadb
acb
acba
acbad
acbd
acbda
acdab
acdb
acdba
adabc
adacb
adbac
adbc
adbca
adcab
adcb
adcba
b
ba
baa
baac
baacd
baadc
bac
baca
bacad
bacd
bacda
badac
badc
badca
bc
bca
bcaa
bcaad
bcad
bcada
bcd
bcda
bcdaa
bdaac
bdac
bdaca
bdc
bdca
bdcaa
c
caab
caabd
caadb
cab
caba
cabad
cabd
cabda
cadab
cadb
cadba
cb
cba
cbaa
cbaad
cbad
cbada
cbd
cbda
cbdaa
cd
cdaab
cdab
cdaba
cdb
cdba
cdbaa
d
daabc
daacb
dabac
dabc
dabca
dacab
dacb
dacba
dbaac
dbac
dbaca
dbc
dbca
dbcaa
dc
dcaab
dcab
dcaba
dcb
dcba
dcbaa


Input:
    aaa
Output:
  a
  aa
  aaa

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