-
[백준] 1062 가르침ALGORITHM/BOJ 2021. 2. 15. 21:18
2021-02-15
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class Main {public static int N, K, answer;public static String arr[];public static char alpha[] = {'b', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};public static boolean vtd[];public static int check() {int ans = 0;for(int i = 0; i < N; i++) {int cnt = 0;for(int j = 0; j < arr[i].length(); j++) {int idx = arr[i].charAt(j) - 'a';if(vtd[idx]) cnt++;}if(cnt == arr[i].length()) ans++;}return ans;}public static void solve(int idx, int cnt) {if(cnt == K-5) {answer = Math.max(answer, check());return;}for(int i = idx; i < alpha.length; i++) {int k = alpha[i] - 'a';if(!vtd[k]) {vtd[k] = true;solve(i, cnt+1);vtd[k] = false;}}}public static void main(String[] args) throws Exception{BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());K = Integer.parseInt(st.nextToken());arr = new String[N];vtd = new boolean[27];answer = 0;for(int i = 0; i < N; i++) {st = new StringTokenizer(bf.readLine().trim());String str = st.nextToken();arr[i] = str.substring(4, str.length()-4);}if(K >= 5) {vtd['a'-'a'] = true;vtd['c'-'a'] = true;vtd['n'-'a'] = true;vtd['i'-'a'] = true;vtd['t'-'a'] = true;solve(0, 0);} else answer = 0;System.out.println(answer);}}cs #문체풀이
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 2110 공유기 설치 (0) 2021.02.23 [백준] 3649 로봇 프로젝트 (0) 2021.02.18 [백준] 1561 놀이공원 (0) 2021.02.13 [백준] 5676 음주 코딩 (0) 2021.02.07 [백준] 14425 문자열 집합 (0) 2021.02.02