카테고리 없음
백준 1062번 가르침 (JAVA)
박카스마시며코딩
2022. 1. 6. 20:14
https://www.acmicpc.net/problem/1062
1062번: 가르침
첫째 줄에 단어의 개수 N과 K가 주어진다. N은 50보다 작거나 같은 자연수이고, K는 26보다 작거나 같은 자연수 또는 0이다. 둘째 줄부터 N개의 줄에 남극 언어의 단어가 주어진다. 단어는 영어 소문
www.acmicpc.net
저는 해당 문제를 브루트 포스를 통해서 문제를 해결하였습니다. 기본적으로 5개는 알아야하기 때문에 k가 5보다 작다면 0을 바로 출력하였습니다.
먼저 조합으로 필수 5개를 제외한 k-5개의 알파벳을 뽑고 각각의 문자열이 뽑지 않은 문자열이 있는지 확인하고 그렇지 않다면 결과값을 하나 올려주어 해당 문제를 해결하였습니다.
package BOJ.Bruteforce;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_1062 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String,Integer> stoi = Integer::parseInt;
String[] commands = br.readLine().split(" ");
int n = stoi.apply(commands[0]);
int k = stoi.apply(commands[1]);
String[] inputs = new String[n];
for(int i = 0 ; i < n ; i++) {
inputs[i] = br.readLine();
}
int result = 0;
if(k < 5){
System.out.println(result);
}else{
boolean[] alpha = new boolean[26];
alpha['a' - 'a'] = true;
alpha['n' - 'a'] = true;
alpha['t' - 'a'] = true;
alpha['i' - 'a'] = true;
alpha['c' - 'a'] = true;
System.out.println(cal(k-5,alpha,0,inputs));
}
}
private static int cal(int depth, boolean[] alpha,int start,String[] inputs) {
if(depth == 0){
int count = 0;
for(String input : inputs){
boolean flag = true;
int size = input.length();
for(int i = 0 ; i < size ; i++){
int now = input.charAt(i) - 'a';
if(!alpha[now]){
flag = false;
break;
}
}
if(flag){
count++;
}
}
return count;
}else{
int result = 0;
for(int i = start ; i < 26 ; i++){
if(!alpha[i]){
alpha[i] = true;
result = Math.max(result,cal(depth-1, alpha,i+1,inputs));
alpha[i] = false;
}
}
return result;
}
}
}