diff --git a/H0ngJu/README.md b/H0ngJu/README.md index 1d6fdf40..d9a098c3 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -9,6 +9,7 @@ | 5차시 | 2024.03.16 | 구현 | [요세푸스 문제](https://www.acmicpc.net/problem/1158) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/161 | | 6차시 | 2024.03.19 | 스택 | [오큰수](https://www.acmicpc.net/problem/17298) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/164 | | 7차시 | 2024.03.22 | DP | [1,2,3 더하기](https://www.acmicpc.net/problem/9095) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/166 | +| 8차시 | 2024.03.16 | DP | [쉬운 계단 수](https://www.acmicpc.net/problem/10844) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/170 | | 9차시 | 2024.03.22 | DP | [RGB거리 2](https://www.acmicpc.net/problem/17404) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/172 | --- diff --git "a/H0ngJu/\354\211\254\354\232\264 \352\263\204\353\213\250 \354\210\230.py" "b/H0ngJu/\354\211\254\354\232\264 \352\263\204\353\213\250 \354\210\230.py" new file mode 100644 index 00000000..7c5631a2 --- /dev/null +++ "b/H0ngJu/\354\211\254\354\232\264 \352\263\204\353\213\250 \354\210\230.py" @@ -0,0 +1,28 @@ +import sys + +DIV = 1_000_000_000 + +N = int(sys.stdin.readline().rstrip()) + +dp = [[0] * 10 for _ in range(N+1)] +answer = 0 + +for i in range(1,N+1): + if i == 1: + for k in range(1,10): + dp[i][k] = 1 + + else: + for num in range(10): + if num == 0: + dp[i][num] = dp[i-1][1]%DIV + elif num == 9: + dp[i][num] = dp[i-1][8]%DIV + else: + dp[i][num] = (dp[i-1][num-1] + dp[i-1][num+1])%DIV + +for k in range(10): + answer += dp[N][k] + +print(answer%DIV) + diff --git a/Munbin-Lee/README.md b/Munbin-Lee/README.md index 1be0e769..5221cadf 100644 --- a/Munbin-Lee/README.md +++ b/Munbin-Lee/README.md @@ -38,5 +38,6 @@ | 35차시 | 2024.02.18 | 백트래킹 | 단어 마방진 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 | | 36차시 | 2024.02.21 | 문자열 | 회문은 회문아니야!! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 | | 37차시 | 2024.03.05 | 백트래킹 | 석유 시추 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 | -| 37차시 | 2024.03.08 | 트라이 | 전화번호 목록 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 | +| 38차시 | 2024.03.08 | 트라이 | 전화번호 목록 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 | +| 39차시 | 2024.03.14 | 문자열 | 물약 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/160 | --- diff --git "a/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" "b/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" new file mode 100644 index 00000000..efcfe576 --- /dev/null +++ "b/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" @@ -0,0 +1,45 @@ +stdin = open(0) + +def input(): + return stdin.readline().rstrip() + +N, M = map(int, input().split()) + +prices = {} + +for _ in range(N): + item, price = input().split() + prices[item] = int(price) + +recipes = [] + +for _ in range(M): + target, formula = input().split('=') + terms = formula.split('+') + recipes.append([target, terms]) + +def updatePrice(target, terms): + price = 0 + + for term in terms: + count = int(term[0]) + item = term[1:] + + if item not in prices: + return + + price += count * prices[item] + + if target not in prices or prices[target] > price: + prices[target] = price + +for _ in range(M): + for recipe in recipes: + updatePrice(recipe[0], recipe[1]) + +if 'LOVE' not in prices: + print('-1') + exit() + +answer = min(prices['LOVE'], 1000000001) +print(answer) diff --git "a/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" "b/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" new file mode 100644 index 00000000..e1cc927a --- /dev/null +++ "b/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" @@ -0,0 +1,50 @@ +# 자물쇠의 중간 부분이 모두 1인지 확인 +def is_valid(new_lock): + length = len(new_lock) // 3 + + for r in range(length, length * 2): + for c in range(length, length * 2): + if new_lock[r][c] != 1: + return False + + return True + +def solution(key, lock): + n = len(lock) + k = len(key) + new_lock = [[0] * (n * 3) for _ in range(n * 3)] + + for r in range(n): + for c in range(n): + new_lock[r + n][c + n] = lock[r][c] + + for _ in range(4): + rev_key = key[::-1] + key = [] + for c in range(k): + row = [] + for r in range(k): + row.append(rev_key[r][c]) + key.append(row) + + """ + 열쇠를 돌리는 로직은 한 줄로도 구현가능 합니다 + key = [row for row in zip(*reversed(key))] + """ + + for r in range(n * 2): + for c in range(n * 2): + # 자물쇠에 열쇠를 끼운다 + for i in range(k): + for j in range(k): + new_lock[r + i][c + j] += key[i][j] + + # 자물쇠에 열쇠가 딱 들어갔는지 확인 + if is_valid(new_lock): + return True + + # 자물쇠에서 열쇠를 빼서 복구시킨다 + for i in range(k): + for j in range(k): + new_lock[r + i][c + j] -= key[i][j] + return False \ No newline at end of file diff --git a/pknujsp/README.md b/pknujsp/README.md index 571b70d7..665f42d8 100644 --- a/pknujsp/README.md +++ b/pknujsp/README.md @@ -38,4 +38,8 @@ | 34차시 | 2024.02.12 | BFS | [이분 그래프](https://www.acmicpc.net/problem/1707) | [#135](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/135) | | 35차시 | 2024.02.18 | 그리디 | [선물할인](https://www.acmicpc.net/problem/25947) | [#137](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137) | | 36차시 | 2024.02.21 | 이진탐색 | [휴게소 세우기](https://www.acmicpc.net/problem/1477) | [#143](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143) | -| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) | \ No newline at end of file +| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) | +| 38차시 | 2024.03.08 | BRUTE_FORCE | [자물쇠와 열쇠](https://school.programmers.co.kr/learn/courses/30/lessons/60059) | [#154](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/154) | +| 39차시 | 2024.03.19 | 큐 | [디스크 컨트롤러](https://school.programmers.co.kr/learn/courses/30/lessons/42627) | [#163](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/163) | +| 40차시 | 2024.03.22 | 그리디 | [센서](https://www.acmicpc.net/problem/2212) | [#168](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/168) | +| 41차시 | 2024.03.25 | 다익스트라 | [알고스팟](https://www.acmicpc.net/problem/1261) | [#169](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/169) | \ No newline at end of file diff --git "a/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" "b/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" new file mode 100644 index 00000000..07494620 --- /dev/null +++ "b/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" @@ -0,0 +1,18 @@ +from sys import * + +N = int(stdin.readline()) +K = int(stdin.readline()) +SENSORS = sorted(set(map(int, stdin.readline().split()))) + +if K >= N: + print(0) + exit() + +distances = [SENSORS[i] - SENSORS[i - 1] for i in range(1, len(SENSORS))] +distances.sort(reverse=True) + +result = 0 +for i in range(K - 1, len(distances)): + result += distances[i] + +print(result) diff --git "a/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" "b/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" new file mode 100644 index 00000000..364af0a3 --- /dev/null +++ "b/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" @@ -0,0 +1,33 @@ +from heapq import * +from sys import * + +C, R = map(int, stdin.readline().split()) +arr = [list(map(int, list(stdin.readline().strip()))) for _ in range(R)] + +drc = ((1,0),(-1,0),(0,1),(0,-1)) +visited = [[False] * C for _ in range(R)] +heap = [(0, 0, 0)] +target_r = R - 1 +target_c = C - 1 + +while heap: + cost, r, c = heappop(heap) + + if r == target_r and c == target_c: + print(cost) + break + if visited[r][c]: + continue + + visited[r][c] = True + + for dr, dc in drc: + nr = r + dr + nc = c + dc + + if not 0 <= nr < R or not 0 <= nc < C: + continue + if visited[nr][nc]: + continue + + heappush(heap, (cost + arr[nr][nc], nr, nc)) diff --git "a/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" "b/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" new file mode 100644 index 00000000..0a0b4d8e --- /dev/null +++ "b/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" @@ -0,0 +1,24 @@ +from heapq import * +from collections import * + +def solution(jobs): + jobs = deque(sorted(jobs)) + jobs_num = len(jobs) + + curr_time = wait_time = 0 + heap = [] + + while heap or jobs: + while jobs and jobs[0][0] <= curr_time: + requested_time, duration = jobs.popleft() + heappush(heap, (duration, requested_time)) + + if heap: + duration, requested_time = heappop(heap) + + curr_time += duration + wait_time += curr_time - requested_time + else: + curr_time += 1 + + return wait_time // jobs_num \ No newline at end of file diff --git "a/tgyuuAn/BFS/\354\227\264\354\207\240.py" "b/tgyuuAn/BFS/\354\227\264\354\207\240.py" new file mode 100644 index 00000000..36494506 --- /dev/null +++ "b/tgyuuAn/BFS/\354\227\264\354\207\240.py" @@ -0,0 +1,97 @@ +import sys +from collections import deque, defaultdict + +def input(): return sys.stdin.readline().rstrip() + +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] + +T = int(input()) + +for _ in range(T): + + H, W = map(int, input().split()) + building = [["." for _ in range(W+2)]] + door_info = defaultdict(set) + keys_i_have = set() + + for row in range(H): + _input = "." + _input += input() + _input += "." + + for col in range(W+2): + if _input[col] not in ("*", ".", "$") and _input[col].isupper(): + door_info[_input[col]].add((row+1, col)) + + building.append(list(_input)) + + building.append(["." for _ in range(W+2)]) + + keys_info = input() + if keys_info != "0": + keys_i_have.update(set(keys_info)) + + answer = 0 + visited = set() + locked_doors_to_access = set() + + deq = deque([(0, 0)]) + while deq: + now_row, now_col = deq.popleft() + + for dir in range(4): + new_row = now_row + dy[dir] + new_col = now_col + dx[dir] + + if new_row < 0 or new_row >= H+2: continue + if new_col < 0 or new_col >= W+2: continue + if (new_row, new_col) in visited: continue + if building[new_row][new_col] == "*": continue + + # print(now_row, now_col,building[new_row][new_col]) + # print(locked_doors_to_access) + # print(keys_i_have) + # print() + + if building[new_row][new_col] == "$": + answer += 1 + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + continue + + # 문을 만났을 경우, 이 때 까지 얻은 열쇠로 열 수 있는 지 확인함. 아닐 경우 접근할 수 있는 문 목록에 추가 + if building[new_row][new_col].isalpha() and building[new_row][new_col].isupper(): + + # 열쇠를 이미 가지고 있는 경우 + if building[new_row][new_col].lower() in keys_i_have: + building[new_row][new_col] = "." + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + + # 열쇠가 없어서 문을 못 열 경우 접근할 수 있는 문 목록에 추가 + else: locked_doors_to_access.add((new_row, new_col)) + + continue + + # 열쇠를 획득했을 경우, 이 때 까지 만난 문들 중에 열 수 있는 것들을 queue에 넣음 + if building[new_row][new_col].isalpha() and building[new_row][new_col].islower(): + keys_i_have.add(building[new_row][new_col]) + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + + for can_open_row, can_open_col in door_info[building[new_row][new_col].upper()]: + if (can_open_row, can_open_col) in locked_doors_to_access: + building[can_open_row][can_open_col] = "." + visited.add((can_open_row, can_open_col)) + deq.append((can_open_row, can_open_col)) + locked_doors_to_access.discard((can_open_row, can_open_col)) + + continue + + # 빈 공간일 경우, 그냥 지나감 + if building[new_row][new_col] == ".": + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + + print(answer) \ No newline at end of file diff --git a/tgyuuAn/DP/KCM Travel.py b/tgyuuAn/DP/KCM Travel.py new file mode 100644 index 00000000..4e279062 --- /dev/null +++ b/tgyuuAn/DP/KCM Travel.py @@ -0,0 +1,30 @@ +from collections import defaultdict, deque +import sys + +def input(): return sys.stdin.readline().rstrip() + +T = int(input()) +for _ in range(T): + N, M, K = map(int, input().split()) + + costs = [[int(1e9) for _ in range(M+1)] for _ in range(N+1)] + costs[1][0] = 0 + + graph = [[] for _ in range(N+1)] + for _ in range(K): + start, destination, cost, duration = map(int,input().split()) + graph[start].append((destination, cost, duration)) + + # print(edges) + + for cost in range(M+1): + for city in range(1, N): + if costs[city][cost] == int(1e9): continue + + for now_destination, now_cost, now_duration in graph[city]: + + if now_cost + cost <= M and costs[now_destination][cost + now_cost] > costs[city][cost] + now_duration: + costs[now_destination][cost + now_cost] = costs[city][cost] + now_duration + + result = min(costs[N]) + print(result) if result != int(1e9) else print("Poor KCM") \ No newline at end of file diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 5fd3dcb3..d180acaa 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -29,22 +29,27 @@ | 25차시 | 2023.12.26 | 구현 | 방의 개수 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/90 | 26차시 | 2023.12.29 | BFS | 백조의 호수 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/95 | 27차시 | 2024.01.01 | BFS | 탈옥 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/96 -| 28차시 | 2023.01.04 | 스택 | 히스토그램에서 가장 큰 직사각형 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99 -| 29차시 | 2023.01.07 | 그리디 | 소트 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103 -| 30차시 | 2023.01.10 | BFS | 아이템 줍기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104 -| 31차시 | 2023.01.13 | DP | 진우의 달 여행 (Large) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105 -| 32차시 | 2023.01.16 | 그리디 | 멀티탭 스케줄링 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108 -| 33차시 | 2023.01.19 | 이분 탐색 | 배열에서 이동 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115 -| 34차시 | 2023.01.22 | 힙 | 가운데를 말해요 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116 -| 35차시 | 2023.01.25 | 이분 탐색 | 공유기 설치 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120 -| 36차시 | 2023.02.04 | BFS | 로봇 청소기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126 -| 37차시 | 2023.02.04 | BFS | 교환 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131 -| 38차시 | 2023.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138 -| 39차시 | 2023.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 -| 40차시 | 2023.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 -| 41차시 | 2023.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 -| 42차시 | 2023.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 +| 28차시 | 2024.01.04 | 스택 | 히스토그램에서 가장 큰 직사각형 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99 +| 29차시 | 2024.01.07 | 그리디 | 소트 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103 +| 30차시 | 2024.01.10 | BFS | 아이템 줍기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104 +| 31차시 | 2024.01.13 | DP | 진우의 달 여행 (Large) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105 +| 32차시 | 2024.01.16 | 그리디 | 멀티탭 스케줄링 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108 +| 33차시 | 2024.01.19 | 이분 탐색 | 배열에서 이동 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115 +| 34차시 | 2024.01.22 | 힙 | 가운데를 말해요 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116 +| 35차시 | 2024.01.25 | 이분 탐색 | 공유기 설치 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120 +| 36차시 | 2024.02.04 | BFS | 로봇 청소기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126 +| 37차시 | 2024.02.04 | BFS | 교환 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131 +| 38차시 | 2024.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138 +| 39차시 | 2024.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 +| 40차시 | 2024.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 +| 41차시 | 2024.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 +| 42차시 | 2024.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 | 43차시 | 2024.03.10 | 이분 탐색 | 징검다리 건너기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157 | 44차시 | 2023.03.13 | 트라이 | 개미굴 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159 | 45차시 | 2023.03.16 | 트라이 | 트라이 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162 +| 46차시 | 2023.03.20 | 트라이 | AB | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/165 +| 47차시 | 2023.03.22 | 수학, 분할정복 | 너 봄에는 캡사이신이 맛있단다 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/167 +| 48차시 | 2023.03.25 | 벨만 포드 | 골목길 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/171 +| 49차시 | 2023.03.29 | DP | KCM Travel | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/174 +| 50차시 | 2024.04.01 | BFS | 열쇠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175 --- diff --git "a/tgyuuAn/\353\262\250\353\247\214 \355\217\254\353\223\234/\352\263\250\353\252\251\352\270\270.py" "b/tgyuuAn/\353\262\250\353\247\214 \355\217\254\353\223\234/\352\263\250\353\252\251\352\270\270.py" new file mode 100644 index 00000000..b879b593 --- /dev/null +++ "b/tgyuuAn/\353\262\250\353\247\214 \355\217\254\353\223\234/\352\263\250\353\252\251\352\270\270.py" @@ -0,0 +1,78 @@ +import sys +from collections import deque + +def input(): return sys.stdin.readline().rstrip() + +N, M = map(int, input().split()) +edge = [[] for _ in range(N+1)] + +# 간선 정보 받음 +for _ in range(M): + start, destination, cost = map(int,input().split()) + edge[start].append([destination, cost]) + +# 초기 세팅 +board = [-int(1e9) for _ in range(N+1)] +board[1] = 0 + +# 최적의 경로를 찾기 위해 역추적 하기 위해서 이전 노드를 기록 +prev_node = [-1 for _ in range(N+1)] +prev_node[1] = 0 + +for _ in range(N-1): + for start in range(1,N+1): + for destination, cost in edge[start]: + if board[destination] < board[start] + cost: + board[destination] = board[start] + cost + prev_node[destination] = start + +has_cycle = False +is_connect_target = False +for start in range(1,N+1): + for destination, cost in edge[start]: + # 사이클 발생 + if board[destination] < board[start] + cost: + has_cycle = True + + # 사이클이 발생해도 경로랑 관련이 없을 수도 있으므로, + # 사이클이 발생한 지점이 목표 지점과 관련이 있는지 체크크 + deq = deque([start]) + visited = {start,} + while deq: + now = deq.popleft() + + for d, c in edge[now]: + if d in visited: continue + + deq.append(d) + visited.add(d) + + # 사이클이 있고 목표지점 혹은 시작지점과 붙어있으면 -1 + if d == 1 or d == N: + is_connect_target = True + break + + if is_connect_target: break + break + +# 사이클이 있는데 해당 사이클이 목표와 연결되어 있을 경우 +if has_cycle and is_connect_target: print(-1) +else: + answer = [] + now = N + while now != 1: + answer.append(now) + now = prev_node[now] + + answer.append(now) + + if now != 1: print(-1) + else: print(*answer[::-1]) + +# 총 간선 = 2만개, +# 총 노드 = 100개 +# 벨만 포드 = ( 간선 X 노드 -1 ) -> 198만 시간 복잡도 가능. + +# 최적의 경로 +# 사이클이 발생해도 갈 수 있을 수 있음. +# 사이클이 없더라도 도달할 수 없을 수 있음. \ No newline at end of file diff --git "a/tgyuuAn/\354\210\230\355\225\231/\353\204\210 \353\264\204\354\227\220\353\212\224 \354\272\241\354\202\254\354\235\264\354\213\240\354\235\264 \353\247\233\354\236\210\353\213\250\353\213\244.py" "b/tgyuuAn/\354\210\230\355\225\231/\353\204\210 \353\264\204\354\227\220\353\212\224 \354\272\241\354\202\254\354\235\264\354\213\240\354\235\264 \353\247\233\354\236\210\353\213\250\353\213\244.py" new file mode 100644 index 00000000..ac903fbb --- /dev/null +++ "b/tgyuuAn/\354\210\230\355\225\231/\353\204\210 \353\264\204\354\227\220\353\212\224 \354\272\241\354\202\254\354\235\264\354\213\240\354\235\264 \353\247\233\354\236\210\353\213\250\353\213\244.py" @@ -0,0 +1,33 @@ +import sys + +DIV = 1_000_000_007 + +def input(): return sys.stdin.readline().rstrip() + +def power(n, over): + if over == 0: return 1 + elif over == 1: return n + elif over %2 == 0: + half = (power(n, over//2) % DIV) + return (half * half) % DIV + else: + half = (power(n, over//2) % DIV) + return (half * half * (n % DIV)) % DIV + +N = int(input()) +numbers = sorted(list(map(int,input().split()))) +DP = [-1 for _ in range(N)] +answer = 0 + +for start_idx in range(N): + start_num = numbers[start_idx] + end_num = numbers[N-start_idx-1] + + # 만약 캐싱이 되어있지 않을 경우 직접 계산 + if DP[N-start_idx-1] == -1: DP[N-start_idx-1] = power(2, N-start_idx-1) + + # 한번이라도 계산 했으면 바로 이용 + answer += ((end_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV + answer -= ((start_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV + +print(answer % DIV) \ No newline at end of file diff --git "a/tgyuuAn/\355\212\270\353\235\274\354\235\264/AB.py" "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/AB.py" new file mode 100644 index 00000000..6ae80188 --- /dev/null +++ "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/AB.py" @@ -0,0 +1,80 @@ +import sys +from collections import defaultdict + +class Node(): + def __init__(self, key = None): + self.key = key + self.count = 0 + self.children = {} + +class Tries(): + def __init__(self): + self.head = Node(None) + + def add(self, element): + now = self.head + + for char in element: + if char not in now.children: + now.children[char] = Node(char) + + now = now.children[char] + now.count += 1 + + def delete(self, element): + now = self.head + + for char in element: + child = now.children[char] + child.count -= 1 + if child.count == 0: del now.children[char] + now = child + + def find(self, element): + now = self.head + dic = defaultdict(int) + + string = "" + for char in element: + if char not in now.children: return dic + + string = string + char + now = now.children[char] + dic[string] = now.count + + return dic + +def input(): return sys.stdin.readline().rstrip() + +def query(method, target, element): + if method == "add": target.add(element) + + if method == "delete": target.delete(element) + + if method == "find": + n = len(element) + a_result = A.find(element) + b_result = B.find(element[::-1]) + + answer = 0 + for a_len in range(1,n): + answer += a_result[element[:a_len]] * b_result[element[:a_len-1:-1]] + + print(answer) + +N = int(input()) +A = Tries() +B = Tries() +method, target, element = "", "", "" + +for _ in range(N): + _input = input() + if _input[:4] == "find": + method,element = _input.split() + query(method, A, element) + + else: + method, target, element = _input.split() + + if target == "A": query(method, A, element) + else: query(method, B, element[::-1]) \ No newline at end of file