[ next_permutation 코드 ]
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int L, C;
vector<char> v(30);
int ch[30];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> L >> C;
for(int i=0;i<C;i++)
cin >> v[i];
fill(ch, ch+C, 1);
for(int i=0;i<L;i++) ch[i] = 0;
sort(v.begin(), v.begin()+C);
do{
bool flagA=false, flagB=false;
int cnt=0;
for(int i=0;i<C;i++)
if(!ch[i]){
if(v[i]=='a' or v[i]=='e' or v[i]=='i' or v[i]=='o' or v[i]=='u'){
flagA = true;
}else{
cnt++;
if(cnt >= 2) flagB = true;
}
}
if(flagA and flagB){
for(int i=0;i<C;i++)
if(!ch[i]) cout << v[i];
cout <<'\n';
}
}while(next_permutation(ch,ch+C));
return 0;
}
[ 백트래킹 코드 ]
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int L, C;
char arr[30];
int isused[30];
vector<char> v(30);
int ch[30];
void func(int depth, int start){
if(depth == L){
bool flagA=false, flagB=false;
int cnt=0;
for(int i=0;i<L;i++)
if(arr[i]=='a' or arr[i]=='e' or arr[i]=='i' or arr[i]=='o' or arr[i]=='u'){
flagA = true;
}else{
cnt++;
if(cnt >= 2) flagB = true;
}
if(flagA and flagB)
{
for(int i=0;i<L;i++)
cout << arr[i];
cout << '\n';
}
}else
{
for(int i=start;i<C;i++)
{
if(isused[i]) continue;
isused[i] = true;
arr[depth] = v[i];
func(depth+1, i+1);
isused[i] = false;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> L >> C;
for(int i=0;i<C;i++)
cin >> v[i];
sort(v.begin(), v.begin()+C);
func(0, 0);
return 0;
}