알고리즘/백준

백준 5568 카드 놓기

등반 2021. 12. 22. 21:41

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

 

5568번: 카드 놓기

예제 1의 경우 상근이는 11, 12, 21, 112, 121, 122, 212를 만들 수 있다.

www.acmicpc.net

만들 수 있는 정수를 모두 구하는 문제다.

지문에 "이렇게 한 정수를 만드는 조합이 여러 가지 일 수 있다."라는 말이 적혀있다.

중복을 없애자.

 

#include <iostream>
using namespace std;

#include <vector>
#include <set>
#include <string>
set<string> ans;

bool visited[11];
void recur(const vector<int>& v, int depth, int K, int now, const string& S){
    if(depth == K){
        ans.insert(S);
    }else{
        for(int i=0; i<v.size(); i++){
            if(visited[i]) continue;
            visited[i] = true;
            string next_S = S + to_string(v[i]);
            recur(v, depth+1, K, i, next_S);
            visited[i] = false;
        }
    }
}
int main(){
    int N, K; cin >> N >> K;
    vector<int> v(N);
    for(int i=0; i<N; i++){
        cin >> v[i];
    }

    recur(v, 0, K, -1, "");
    cout << ans.size();
    return 0;
}

set에 다 때려넣는 것은 중복을 없애는 좋은 방법이다. 

'알고리즘 > 백준' 카테고리의 다른 글

백준 11899 괄호 끼워넣기  (0) 2021.12.23
백준 3986 좋은 단어  (0) 2021.12.23
백준 18511 큰 수 구성하기  (0) 2021.12.22
백준 17478 재귀함수가 뭔가요?  (0) 2021.12.22
백준 6603 로또  (0) 2021.12.22