[백준/C++] 1759번: 암호 만들기

-inn·2022년 1월 26일
0

백준

목록 보기
18/28

방법

조합 (next_permutation)

  1. 오름차순 정렬된 값만 사용 가능
  2. 중복 제외한 순열 출력

ex) 6C4

  1. 순서 상관 없이 출력
    : {f, f, t, t, t, t} t인 경우, 출력
  2. 순서대로 출력
    : {f, f, f, f, t, t} f인 경우, 출력

코드

#include<bits/stdc++.h>
using namespace std;

int L, C;
vector<char> pw;
vector<bool> tmp;

// 최소 한 개의 모음과 최소 두 개의 자음
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> L >> C;
	char c;
	for (int i = 0; i < C; i++) {
		cin >> c;
		pw.push_back(c);
		tmp.push_back(true);
	}
	sort(pw.begin(), pw.end());

	// 6C4 : {f,f,f,f,t,t} 순서대로 출력하기 위함
	for (int i = 0; i < L; i++) {
		tmp[i] = false;
	}

	do {
		int x = 0, y = 0;
		string s;
		for (int i = 0; i < C; i++) {
			if (!tmp[i]) {
				if (pw[i] == 'a' || pw[i] == 'e' || pw[i] == 'i' 
					|| pw[i] == 'o' || pw[i] == 'u') x++;
				else y++;
				s += pw[i];
			}
			if (i == C - 1) {
				if (x > 0 && y > 1) cout << s << "\n";
			}
		}
	} while (next_permutation(tmp.begin(), tmp.end()));

	return 0;
}
profile
☁️

0개의 댓글