-
백준 BOJ(1062) 가르침알고리즘 풀이/백준(Boj) 2020. 2. 22. 18:18
문제 :
https://www.acmicpc.net/problem/1062
1062번: 가르침
첫째 줄에 단어의 개수 N과 K가 주어진다. N은 50보다 작거나 같은 자연수이고, K는 26보다 작거나 같은 자연수 또는 0이다. 둘째 줄부터 N개의 줄에 남극 언어의 단어가 주어진다. 단어는 영어 소문자로만 이루어져 있고, 길이가 8보다 크거나 같고, 15보다 작거나 같다. 모든 단어는 중복되지 않는다.
www.acmicpc.net
풀이 :
anta와 tica는 무조건 들어가야 하므로 최소 k가 5개는 필요하다 5개가 되지 않는다면 0을 리턴하자
5개가 넘는다면 a n t i c는 true 처리를 해주고 나머지 21개의 알파벳 중 k-5개를 고른다.
k-5개를 골랐다면 check만을 통해 단어를 만들 수 있는지 확인하고 최대 값을 구해준다.
코드 ( C++ )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <cstring> #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; // a n t i c bool check[26]; vector<string> v; int ans; void solve(int begin, int cnt) { if (cnt == 0) { int sum = 0; //비교해서 최댓값 구한다 for (int i = 0; i < v.size(); ++i) { bool ch = true; for (int j = 0; j < v[i].size(); ++j) { int temp = v[i][j] - 'a'; if (check[temp] == 0) { ch = false; break; } } if (ch) sum++; } ans = max(ans, sum); return; } for (int i = begin + 1; i < 26; ++i) { if (check[i]) continue; check[i] = true; solve(i, cnt - 1); check[i] = false; } } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) { string s; cin >> s; v.push_back(s); } if (k < 5) { cout << 0 << "\n"; return 0; } k -= 5; check['a' - 'a'] = true; check['n' - 'a'] = true; check['t' - 'a'] = true; check['i' - 'a'] = true; check['c' - 'a'] = true; solve(-1, k); cout << ans << "\n"; return 0; } '알고리즘 풀이 > 백준(Boj)' 카테고리의 다른 글
백준(BOJ) 1175번 배달 (0) 2020.03.08 백준( BOJ ) 16236번 아기 상어 (0) 2020.02.24 백준(BOJ) 17406번 배열 돌리기 4 (0) 2020.02.22 백준(BOJ) 17281번 ⚾ (0) 2020.02.20 백준(BOJ) 17837번 새로운 게임 2 (0) 2020.02.11