# 백준 : 암호 만들기 (1759)

문제

https://www.acmicpc.net/problem/1759

소스

(Github: https://github.com/wonjnlee/wallydev/blob/master/bj_1759_makecrypto)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <list>
#include <cmath>
using namespace std;

int L, C;
char alphabet[16];

void DFS(int idx, int cnt, int mo, int ja, string str) {
    if(cnt == L) {
        if ((mo >= 1) && (ja >= 2))
            cout<<str<<endl;
        return;
    }
    if(idx == C) return;
    if((alphabet[idx] == 'a') || (alphabet[idx] == 'e') || (alphabet[idx] == 'i') || (alphabet[idx] == 'o') || (alphabet[idx] == 'u')) {
        DFS(idx+1, cnt+1, mo+1, ja, str + alphabet[idx]);
    }
    else {
        DFS(idx+1, cnt+1, mo, ja+1, str + alphabet[idx]);
    }
    DFS(idx+1, cnt, mo, ja, str);
}

int main(void)
{
    cin>>L>>C;
    for(int i=0; i<C; i++)  cin>>alphabet[i];
    sort(alphabet, alphabet + C);
    DFS(0,0,0,0,"");
    return 0;
}

풀이

백트래킹 공부중이라서 풀어본 문제.
아직 쉽게 접근이 되지는 않는다.. 이 문제는 '뒤로 돌아갈 필요가 없다'는게 함정인데
말그대로 문자열을 만들던지 안만들던지의 차이지 그걸 지우고 안하고 그럴 필요가 없다는 것..
그 기준을 잡는 것이 쉽지 않다. 좀 더 많은 연습이 필요한듯!

댓글