지코바) 6월 1째주 문제풀이

상수·2022년 6월 3일
0

11726) 2×n 타일링

from sys import stdin

input = stdin.readline

tiling_method = []

n = int(input().rstrip())

for index in range(n+1):
  if index < 3:
    tiling_method.append(index)
  else:
    tiling_method.append(tiling_method[index-1] + tiling_method[index-2])

print(tiling_method[n] % 10007)

1541) 잃어버린 괄호

import sys

input = sys.stdin.readline

exp = input().rstrip().split('-')

if len(exp[0]) == 0:
  exp[0] = '0'

res = 0

for index in range(len(exp)):
  temp_res = 0
  temp = list(map(lambda x: int(x.lstrip('0')), exp[index].split('+')))

  for el in temp:
    temp_res = temp_res + el

  if index == 0:
    res = res + temp_res
  else:
    res = res - temp_res

print(res)

10816) 숫자 카드 2

import sys

input = sys.stdin.readline

n = int(input().rstrip())

cards = list(map(str, input().split()))

cards_dict = {}

for index in range(n):
  if cards[index] in cards_dict:
    cards_dict[cards[index]] = cards_dict[cards[index]] + 1
  else:
    cards_dict[cards[index]] = 1

m = int(input().rstrip())

query_cards = list(map(str, input().split()))

output = []

for index in range(m):
  if query_cards[index] in cards_dict:
    output.append(str(cards_dict[query_cards[index]]))
  else:
    output.append(str(0))

print(' '.join(output))

1620) 나는야 포켓몬 마스터 이다솜

import sys

pokedex = {}
input = sys.stdin.readline

n, m = map(int, input().split())

for index in range(1, n+1):
  pokemon = input().rstrip()
  pokedex[str(index)] = pokemon
  pokedex[pokemon] = str(index)

for _ in range(m):
  query = input().rstrip()
  print(pokedex[query])

1260) DFS와 BFS

from sys import stdin

input = stdin.readline

graph = []
dfs_visited = []
bfs_visited = []
dfs_result = []
bfs_result = []

def dfs(graph, v, n):
  dfs_visited[v] = True
  dfs_result.append(v)
  for w in range(1, n+1):
    if graph[v][w] == 1 and graph[w][v] == 1 and not dfs_visited[w]:
      dfs(graph, w, n)

def bfs(graph, v, n):
  q = []
  bfs_visited[v] = True
  q.append(v)

  while len(q) > 0:
    v = q.pop(0)
    bfs_result.append(v)
    for w in range(1, n+1):
      if graph[v][w] == 1 and graph[w][v] == 1 and not bfs_visited[w]:
        q.append(w)
        bfs_visited[w] = True

n, m, v = list(map(int, input().rstrip().split()))

for _ in range(n+1):
  graph.append([0 for _ in range(n+1)])
  dfs_visited.append(False)
  bfs_visited.append(False)

for index in range(m):
  v1, v2 = list(map(int, input().rstrip().split()))
  graph[v1][v2] = 1
  graph[v2][v1] = 1

dfs(graph, v, n)
bfs(graph, v, n)

print(' '.join(list(map(str, dfs_result))))
print(' '.join(list(map(str, bfs_result))))
profile
Dart와 TypeScript를 다룹니다.

0개의 댓글