알고리즘

[알고리즘] 백준 1062 - 가르침

blueberrysoda 2024. 7. 22. 21:23
#include <iostream>
#include <vector>
using namespace std;

int N, K;
vector<string> V;
bool alpha[26];
int Ans;

void solve(int idx, int cnt){
    if(cnt == 0){
        int tmp = 0;
        for(int i=0; i<V.size(); i++){
            bool flag = true;
            for(int j=0; j<V[i].size(); j++){
                if(alpha[V[i][j] - 'a'] == false){
                    flag = false;
                    break;
                }
            }
            if(flag == true){
                tmp++;
            }
        }
        Ans = max(Ans, tmp);
        return;
    }

    for(int i=idx; i<26; i++){
        if(alpha[i] == true) continue;
        alpha[i] = true;
        solve(i, cnt - 1);
        alpha[i] = false;
    }
    
    return;
}

int main(){
    cin >> N >> K;
    string S;
    for(int i=0; i<N; i++){
        cin >> S;
        V.push_back(S.substr(4, S.size() - 8));
    }

    if(K < 5){
        cout << 0 << "\n";
        return 0;
    }

    alpha[0] = alpha[2] = alpha[8] = alpha[13] = alpha[19] = true;

    solve(0, K - 5);
    cout << Ans << "\n";
    return 0;
}