Skip to content

Commit 89cae7e

Browse files
authored
Merge pull request #905 from 6047198844/master
[boj]부분수열의 합 / [boj]퇴사 / [boj]가르침 / [boj]알파벳
2 parents 1db5a34 + d056690 commit 89cae7e

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

BOJ/1062.가르침/6047198844.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import re
2+
3+
def count_bit(n):
4+
return n % 2 + count_bit(n // 2) if n >= 2 else n
5+
6+
N, K = map(int, input().split())
7+
words = [set(re.findall(r'[^acnit]',input())) for _ in range(N)]
8+
union_words = set()
9+
for word in words:
10+
union_words |= word
11+
12+
if K < 5:
13+
print(0)
14+
exit()
15+
16+
K = K - 5
17+
word_n = len(union_words)
18+
union_words = list(union_words)
19+
20+
#K는 최대로 배울수있는 단어수이다. 만약에 배워야하는 단어의수가 K보다 같거나 작으면 모든 단어를 배울수있다는뜻이다.
21+
if K >= word_n:
22+
print(len(words))
23+
exit()
24+
25+
#최대로 배울수있는 단어의 수만큼 배운다.
26+
res = 0
27+
for i in range(1 << word_n):
28+
if count_bit(i) == K:
29+
choosen_alpha = set()
30+
for j in range(word_n):
31+
if i & 1 << j:
32+
choosen_alpha.add(union_words[j])
33+
cnt = 0
34+
for word in words:
35+
if word == word & choosen_alpha:
36+
cnt += 1
37+
res = max(res, cnt)
38+
39+
print(res)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from itertools import combinations
2+
3+
#idx를 뽑거나 뽑지 않는 경우의수.
4+
#N개를 뽑았을때 합이 S면 True, 아니면 False반환
5+
#idx 현재 판단의 기준이되는 idx / 목표하는 합 S / 누적합 partial_sum
6+
def partial_sequence(idx:int, N:int, S:int, partial_sum:int, sequence:list):
7+
if N < 0:
8+
return 0
9+
10+
if idx == len(sequence):
11+
if partial_sum == S and N == 0:
12+
return 1
13+
return 0
14+
15+
res = 0
16+
res += partial_sequence(idx+1, N-1, S, partial_sum+sequence[idx], sequence)
17+
res += partial_sequence(idx+1, N, S, partial_sum, sequence)
18+
return res
19+
20+
N, S = map(int, input().split())
21+
sequence = list(map(int, input().split()))
22+
23+
res = 0
24+
for _ in range(1,N+1):
25+
res += partial_sequence(0, _, S, 0, sequence)
26+
print(res)

BOJ/14501.퇴사/6047198844.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 현재 시점을 기점으로 만들수있는 최대 이익을 반환한다.
2+
def dfs(start_day):
3+
# 퇴사했으므로 일을 시작할 수 없다.
4+
if start_day == N + 1:
5+
return 0
6+
7+
global memo
8+
# 현재 시점이 저장되어 있는가?
9+
if memo[start_day] != -1:
10+
return memo[start_day]
11+
12+
# 현재 시점의 누적금액. 아직 일을 진행하지 않은 상태이다.
13+
res = 0
14+
15+
# 현재시점을 진행하는데 드는 비용 = (시간비용, 돈비용)
16+
time_cost, money_cost = bill[start_day]
17+
18+
# 기간내에 일을 끝내고 난 후의 범위
19+
for day in range(time_cost + start_day, N + 2):
20+
res = max(res, money_cost + dfs(day))
21+
22+
memo[start_day] = res
23+
return memo[start_day]
24+
25+
26+
N = int(input())
27+
28+
# (시간 , 비용)
29+
bill = [(1, 0)] + [tuple(map(int, input().split())) for _ in range(N)]
30+
memo = [-1] * (N + 1)
31+
print(dfs(0))

BOJ/1987.알파벳/6047198844.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#y,x좌표에서 중복없이 갈수있는 최대의 수.
2+
from functools import lru_cache
3+
4+
R, C = map(int, input().split())
5+
board = [list(input())for _ in range(R)]
6+
path = set()
7+
8+
9+
def dfs(y, x):
10+
res = 1
11+
path.add(board[y][x])
12+
for dy, dx in (-1,0), (1,0), (0,-1), (0,1):
13+
ny = y + dy
14+
nx = x + dx
15+
if 0 <= ny < R and 0 <= nx < C and board[ny][nx] not in path:
16+
res = max(res, 1 + dfs(ny, nx))
17+
path.remove(board[y][x])
18+
return res
19+
20+
print(dfs(0, 0))

0 commit comments

Comments
 (0)