Output:
Code: Select all
AB
Aa
Ab
Ba
Bb
ab
ABa
ABb
Aab
Bab
BCD
BCE
BCa
BDE
BDa
BEa
CDE
CDa
CEa
DEa
aa
aa
ab
bb
BB
Ba
aa
Moderator: Board moderators
Code: Select all
AB
Aa
Ab
Ba
Bb
ab
ABa
ABb
Aab
Bab
BCD
BCE
BCa
BDE
BDa
BEa
CDE
CDa
CEa
DEa
aa
aa
ab
bb
BB
Ba
aa
Code: Select all
#include<iostream>
#include<stdio.h>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
set<string> getcombo(string input,int r)
{
int n = input.length();
set<string> combinations;
for(int i = 0;i <= (n-r);i++)
{
string temp = input.substr(i,r);
for(int j = 0;j < n;j++)
{
if(j >= i && j < i + r)
continue;
for(int k = 0;k < r;k++)
{
string copy(temp);
char c = input[j];
copy[k] = c;
sort(copy.begin(),copy.end());
combinations.insert(copy);
}
}
sort(temp.begin(),temp.end());
combinations.insert(temp);
}
return combinations;
}
int main()
{
string input;
int r;
while(cin >> input >> r)
{
set<string> output = getcombo(input,r);
set<string>::const_iterator iter;
for(iter = output.begin();iter != output.end(); iter++)
{
cout << *iter << "\n";
}
}
return 0;
}
Code: Select all
Accepted......... :)