티스토리 뷰

반응형


#. Problem


* The copyright in this matter is in BOJ



#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

Dictionary 자료구조를 사용하면 쉽게 해결해낼 수 있는 문제다.

Dictionary 의 특성과 사용법을 잘 파악하고 풀어보자...


{팀명1 : [멤버1, 멤버2, ...], 팀명2 : [멤버1, 멤버2, ..]}

{멤버1 : 팀명1, 멤버2 : 팀명2, .. }


이렇게 팀명과 멤버 목록을 리스트로 저장한 dictionray 1

각 멤버에 해당하는 팀명을 저장한 dictionary 2 

가 필요할 듯 하다.


#. Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
N, M = map(int, input().split())
 
team_mem, mem_team = {}, {}
 
for i in range(N):
    teamName, memNum = input(), int(input())
    team_mem[teamName] = []
    for j in range(memNum):
        memName = input()
        team_mem[teamName].append(memName)
        mem_team[memName] = teamName
 
for i in range(M):
    name, q = input(), int(input())
    if q :
        print(mem_team[name])
    else:
        for mem in sorted(team_mem[name]):
            print(mem)
cs


line 3) 빈 dictionary 생성

line 5~11) 걸그룹 수만큼 반복

line 6) 팀명과 멤버의 수를 입력받음

line 7) team_mem dictionary에 입력받은 teamName(key)의 value를 빈 리스트로 초기화

line 8~11) 멤버 수만큼 반복

line 9) 멤버 수를 입력받고

line 10) 해당 팀의 list 형식인 value의 append하여 멤버를 추가

line 11) mem_team dictionary도 마찬가지로 해당 멤버의 팀명을 저장

line 13~19) 문제의 수만큼 반복

line 15~19) 1이 입력되었다면 해당 멤버의 팀명을 출력하고,

    0이 입력되었다면 해당 팀의 멤버들을 모두 출력

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday