Problem Solving/Baekjoon

[백준 / BOJ] 1062 가르침

msmn 2021. 7. 9. 21:43
728x90

알고리즘 분류: 비트마스킹, 백트래킹

문제 링크: https://www.acmicpc.net/problem/1062

 

1062번: 가르침

첫째 줄에 단어의 개수 N과 K가 주어진다. N은 50보다 작거나 같은 자연수이고, K는 26보다 작거나 같은 자연수 또는 0이다. 둘째 줄부터 N개의 줄에 남극 언어의 단어가 주어진다. 단어는 영어 소문

www.acmicpc.net

#include <bits/stdc++.h>
#define ll long long
using namespace std;
// https://www.acmicpc.net/problem/1062 가르침

int word[50];
int alpa;
int n, k, ans;

void go(int start, int depth) {
    if(depth == k) {
        int cnt = 0;
        for(int i=0; i<n; i++) {
            if((alpa & word[i]) == word[i]) cnt++;
        }
        ans = max(ans, cnt);
        return;
    }
    for(int i=start; i<26; i++) {
        if(alpa & (1 << i)) continue;
        alpa |= (1 << i);
        go(i+1, depth+1);
        alpa &= ~(1 << i);
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n >> k;
    if(k < 5) {
        cout << 0;
        return 0;
    }
    if(k == 26) {
        cout << n;
        return 0;
    }
    string str = "antic";
    for(char c : str) alpa |= 1 << (c - 'a');
    
    for(int i=0; i<n; i++) {
        string s;
        cin >> s;
        for(char c : s) word[i] |= 1 << (c - 'a');
    }
    go(0, 5); // 알파벳 5개 이상
    cout << ans;
    
    return 0;
}

 

728x90

'Problem Solving > Baekjoon' 카테고리의 다른 글

[백준 / BOJ] 1103 게임  (0) 2021.07.09
[백준 / BOJ] 1713 후보 추천하기  (0) 2021.07.09
[백준 / BOJ] 3055 탈출  (0) 2021.07.08
[백준 / BOJ] 3425 고스택  (0) 2021.07.08
[백준 / BOJ] 2352 반도체 설계  (0) 2021.03.28