백준
1. 비트 마스킹을 이용한 풀이
참고
import sys, itertools
n,m = map(int, sys.stdin.readline().split())
words = [0] * n
ans = 0
for i in range(n):
temp = sys.stdin.readline().rstrip()
for x in temp:
words[i] |= (1 << (ord(x) - ord('a')))
if m < 5:
print(0)
else:
candidiate = ['b','d','e','f','g','h','j','k','l','m','o','p','q','r','s','u','v','w','x','y','z']
need = ['a','c','t','i','n']
for i in list(itertools.combinations(candidiate, m - 5)):
each = 0
res = 0
for j in need:
each |= (1 << (ord(j) - ord('a')))
for j in i:
each |= (1 << (ord(j) - ord('a')))
for j in words:
if each & j == j:
res += 1
if ans < res:
ans = res
print(ans)
2. DFS를 이용한 풀이
참고
import sys
n, k = map(int, input().split())
if k < 5:
print(0)
exit()
elif k == 26:
print(n)
exit()
answer = 0
words = [set(sys.stdin.readline().rstrip()) for _ in range(n)]
learn = [0] * 26
for c in ('a', 'c', 'i', 'n', 't'):
learn[ord(c) - ord('a')] = 1
def dfs(idx, cnt):
global answer
if cnt == k - 5:
readcnt = 0
for word in words:
check = True
for w in word:
if not learn[ord(w) - ord('a')]:
check = False
break
if check:
readcnt += 1
answer = max(answer, readcnt)
return
for i in range(idx, 26):
if not learn[i]:
learn[i] = True
dfs(i, cnt + 1)
learn[i] = False
dfs(0, 0)
print(answer)