|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int N,K; |
| 7 | + static boolean[] teach = new boolean[26]; |
| 8 | + static boolean[] shouldTeach = new boolean[26]; |
| 9 | + static int shouldTeachCnt; |
| 10 | + static List<Set<Character>> words = new ArrayList<>(); |
| 11 | + static int ans = 0; |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + N = Integer.parseInt(st.nextToken()); |
| 17 | + K = Integer.parseInt(st.nextToken()); |
| 18 | + |
| 19 | + if(K < 5){ |
| 20 | + bw.write("0"); |
| 21 | + bw.close(); |
| 22 | + return; |
| 23 | + } |
| 24 | + K -= 5; |
| 25 | + teach['a' - 'a'] = true; |
| 26 | + teach['c' - 'a'] = true; |
| 27 | + teach['i' - 'a'] = true; |
| 28 | + teach['n' - 'a'] = true; |
| 29 | + teach['t' - 'a'] = true; |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + for (int i = 0; i < N; i++) { |
| 34 | + String str = br.readLine(); |
| 35 | + Set<Character> temp = new HashSet<>(); |
| 36 | + for (int j = 4; j < str.length() - 4; j++) { |
| 37 | + shouldTeach[str.charAt(j) - 'a'] = true; |
| 38 | + temp.add(str.charAt(j)); |
| 39 | + } |
| 40 | + words.add(temp); |
| 41 | + } |
| 42 | + |
| 43 | + for (int i = 0; i < shouldTeach.length; i++) { |
| 44 | + if(shouldTeach[i] && !teach[i]) shouldTeachCnt++; |
| 45 | + } |
| 46 | + |
| 47 | + if(shouldTeachCnt <= K){ |
| 48 | + bw.write(N + ""); |
| 49 | + bw.close(); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + int startPoint = 1; |
| 54 | + while(startPoint < 26 && (!shouldTeach[startPoint] || teach[startPoint])) startPoint++; |
| 55 | + backTracking(startPoint, 0); |
| 56 | + bw.write(ans+""); |
| 57 | + bw.close(); |
| 58 | + } |
| 59 | + static void backTracking(int start, int depth){ |
| 60 | + if(depth == K){ |
| 61 | + ans = Math.max(ans, canReadCount()); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + for (int i = start; i < 26; i++) { |
| 66 | + if(shouldTeach[i] && !teach[i]){ |
| 67 | + teach[i] = true; |
| 68 | + backTracking(i+1, depth+1); |
| 69 | + teach[i] = false; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + static int canReadCount(){ |
| 74 | + int cnt = 0; |
| 75 | + a: for (Set<Character> s : words) { |
| 76 | + for (Character c : s) { |
| 77 | + if(!teach[c - 'a']){ |
| 78 | + continue a; |
| 79 | + } |
| 80 | + } |
| 81 | + cnt++; |
| 82 | + } |
| 83 | + return cnt; |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | +``` |
0 commit comments