From ab036e6d644a0db4b1e9ded48f5e99fb70a5e098 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Thu, 14 Aug 2025 21:23:27 +0900 Subject: [PATCH] =?UTF-8?q?[20250814]=20BOJ=20/=20G4=20/=20=EA=B0=80?= =?UTF-8?q?=EB=A5=B4=EC=B9=A8=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \352\260\200\353\245\264\354\271\250.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "lkhyun/202508/14 BOJ G4 \352\260\200\353\245\264\354\271\250.md" diff --git "a/lkhyun/202508/14 BOJ G4 \352\260\200\353\245\264\354\271\250.md" "b/lkhyun/202508/14 BOJ G4 \352\260\200\353\245\264\354\271\250.md" new file mode 100644 index 00000000..81e95c28 --- /dev/null +++ "b/lkhyun/202508/14 BOJ G4 \352\260\200\353\245\264\354\271\250.md" @@ -0,0 +1,87 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N,K; + static boolean[] teach = new boolean[26]; + static boolean[] shouldTeach = new boolean[26]; + static int shouldTeachCnt; + static List> words = new ArrayList<>(); + static int ans = 0; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + if(K < 5){ + bw.write("0"); + bw.close(); + return; + } + K -= 5; + teach['a' - 'a'] = true; + teach['c' - 'a'] = true; + teach['i' - 'a'] = true; + teach['n' - 'a'] = true; + teach['t' - 'a'] = true; + + + + for (int i = 0; i < N; i++) { + String str = br.readLine(); + Set temp = new HashSet<>(); + for (int j = 4; j < str.length() - 4; j++) { + shouldTeach[str.charAt(j) - 'a'] = true; + temp.add(str.charAt(j)); + } + words.add(temp); + } + + for (int i = 0; i < shouldTeach.length; i++) { + if(shouldTeach[i] && !teach[i]) shouldTeachCnt++; + } + + if(shouldTeachCnt <= K){ + bw.write(N + ""); + bw.close(); + return; + } + + int startPoint = 1; + while(startPoint < 26 && (!shouldTeach[startPoint] || teach[startPoint])) startPoint++; + backTracking(startPoint, 0); + bw.write(ans+""); + bw.close(); + } + static void backTracking(int start, int depth){ + if(depth == K){ + ans = Math.max(ans, canReadCount()); + return; + } + + for (int i = start; i < 26; i++) { + if(shouldTeach[i] && !teach[i]){ + teach[i] = true; + backTracking(i+1, depth+1); + teach[i] = false; + } + } + } + static int canReadCount(){ + int cnt = 0; + a: for (Set s : words) { + for (Character c : s) { + if(!teach[c - 'a']){ + continue a; + } + } + cnt++; + } + return cnt; + } + +} +```