์ ๋ต ์ฝ๋
import java.util.*;
public class BOJ_1062 {
static int max = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
String[] words = new String[n]; //๋จ์ด ์ ์ฅ
ArrayList<Character> list = new ArrayList<>(); //๋ฌธ์ ์ ์ฅ
//๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐฐ์ฐ๋ ๋ฌธ์๋ค ๋จผ์ ์ถ๊ฐ
HashSet<Character> learned = new HashSet<>(Arrays.asList('a', 'n', 't', 'c', 'i'));
for (int i = 0; i < n; i++) {
words[i] = sc.next();
for (char ch : words[i].toCharArray()) {
if (!learned.contains(ch)) { //๊ธฐ๋ณธ๋ฌธ์ ์ ์ธ
if (!list.contains(ch)) { //์ค๋ณต๋์ง ์๋ํ๋ฉด
list.add(ch); //๋๋จธ์ง ๋ฌธ์๋ฅผ ์ ์ฅ
}
}
}
}
//k<5๋ฉด ์๋ฌด๊ฒ๋ ์ฝ์ ์ ใดใด
if (k < 5) {
System.out.println(0);
return;
}
//๋ฐฐ์์ผ ํ๋ ๋ฌธ์ ์(list.size()) <= ๋ฐฐ์ธ์์๋ ๋ฌธ์์(k-5)๋ฉด ๋ค ์ฝ์ ์ ์์
if (list.size() <= k - 5) {
System.out.println(n);
return;
}
// ์กฐํฉ ์์ฑ ๋ฐ ์ต๋ ์ฝ์ ์ ์๋ ๋จ์ด ๊ณ์ฐ
combine(list, learned, words, 0, 0, k - 5);
System.out.println(max);
}
static void combine(ArrayList<Character> list, HashSet<Character> learned, String[] words, int index, int depth, int maxDepth) {
//depth๋ ์๋ก ๋ฐฐ์ด ๋ฌธ์ ์, maxDepth๋ ๋ฐฐ์ธ ์ ์๋ ์ต๋ ๋ฌธ์ ์
//๋ ์๊ฐ ๊ฐ์์ง๋ฉด ์ฝ์ ์ ์๋ ๋จ์ด ๊ฐ์ ๊ตฌํจ
if (depth == maxDepth) {
max = Math.max(max, countWords(words, learned));
return;
}
for (int i = index; i < list.size(); i++) {
learned.add(list.get(i)); //์์๋๋ก ํ๋์ฉ ๋ฐฐ์ฐ๊ธฐ
combine(list, learned, words, i + 1, depth + 1, maxDepth);
learned.remove(list.get(i));
}
}
static int countWords(String[] words, HashSet<Character> learned) {
int count = 0;
for (String word : words) {
boolean readable = true;
for (char ch : word.toCharArray()) {
if (!learned.contains(ch)) { //๋ฐฐ์ฐ์ง ์์ ๋ฌธ์๋ฉด
readable = false; //๋ฒ๋ฆฐ๋ค...
break;
}
}
if (readable) count++;
}
return count;
}
}
40๋ถ
๋ธ๋ฃจํธํฌ์ค
์ผ๋จ a, n, t, c, i ์ด ๋ค์ฏ ๊ฐ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐฐ์ด๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
๊ทธ๋ฌ๋ฉด ๋ฐฐ์ธ ์ ์๋ ์ต๋ ๋จ์ด ์ = k-5๊ฒ ์ง...
list์ '๋ฐฐ์์ผ ํ ๋ฌธ์'๋ฅผ ์ ์ฅํ๋ค.
๊ทธ ๋ฌธ์๋ค์ ์ต๋ํ ๋ฐฐ์ฐ๊ณ ๋์ learned์ ์ถ๊ฐ, ์ฝ์ ์ ์๋ ๋จ์ด ์๋ฅผ ์ฐพ๋๋ค...๋
//๋ ์๊ฐ ๊ฐ์์ง๋ฉด ์ฝ์ ์ ์๋ ๋จ์ด ๊ฐ์ ๊ตฌํจ
if (depth == maxDepth) {
max = Math.max(max, countWords(words, learned));
return;
}
for (int i = index; i < list.size(); i++) {
learned.add(list.get(i)); //์์๋๋ก ํ๋์ฉ ๋ฐฐ์ฐ๊ธฐ
combine(list, learned, words, i + 1, depth + 1, maxDepth);
learned.remove(list.get(i));
}
for (String word : words) {
boolean readable = true;
for (char ch : word.toCharArray()) {
if (!learned.contains(ch)) { //๋ฐฐ์ฐ์ง ์์ ๋ฌธ์๋ฉด
readable = false; //๋ฒ๋ฆฐ๋ค...
break;
}
}
if (readable) count++;
}
์ด๊ฒ๋ ๋ง์๋ง ๋จน์ผ๋ฉด ๋ ๋ก ๋จน๋ ๊ณจ๋4!