-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from wellFoundedDevelopers/jimin/54week
[이지민] - 항체 인식, 토마토, 우체국, 문자열 게임 2
- Loading branch information
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
''' | ||
[문자열 게임 2](https://www.acmicpc.net/problem/20437) | ||
알파벳 배열을 만들어서 문자열 W의 각 문자 위치를 담았다. | ||
이를 이용해서 해당 문자를 K개 포함할 때 최소와 최대를 업데이트 해주었다. | ||
''' | ||
|
||
|
||
import sys | ||
|
||
t = int(sys.stdin.readline()) | ||
|
||
for i in range(t): | ||
w = sys.stdin.readline() | ||
k = int(sys.stdin.readline()) | ||
|
||
alphabet = [[] for _ in range(26)] | ||
|
||
for i in range(len(w) - 1): | ||
alphabet[ord(w[i]) - ord('a')].append(i) | ||
|
||
mini = 10_001 | ||
maxi = -1 | ||
|
||
for i in range(26): | ||
if len(alphabet[i]) >= k: | ||
for j in range(0, len(alphabet[i]) - k + 1): | ||
mini = min(mini, alphabet[i][j + k - 1] - alphabet[i][j] + 1) | ||
maxi = max(maxi, alphabet[i][j + k - 1] - alphabet[i][j] + 1) | ||
|
||
if maxi == -1: | ||
print(-1) | ||
else: | ||
print(mini, maxi) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
''' | ||
[우체국](https://www.acmicpc.net/problem/2141) | ||
https://tussle.tistory.com/1050 블로그 참고 | ||
왼쪽 오른쪽 사람 수가 균일한 위치를 찾는다. | ||
이를 위해서 입력받을때 전체 사람수 total을 만들고, | ||
마을을 순차탐색하면서 왼쪽과 오른쪽 사람의 차이가 가장 적은 위치를 구한다. | ||
''' | ||
|
||
import sys | ||
|
||
n = int(sys.stdin.readline()) | ||
town = [] | ||
total = 0 | ||
for i in range(n): | ||
x, a = map(int, sys.stdin.readline().split()) | ||
total += a | ||
town.append([x, a]) | ||
|
||
town.sort(key= lambda x: x[0]) | ||
|
||
people = 0 | ||
mini = 1_000_000_000 * 100_000 | ||
result = -1 | ||
for i in range(n): | ||
if abs(people - (total - people - town[i][1])) < mini: | ||
mini = abs(people - (total - people - town[i][1])) | ||
result = town[i][0] | ||
|
||
people += town[i][1] | ||
|
||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
''' | ||
[토마토](https://www.acmicpc.net/problem/7576) | ||
bfs를 돌면서 토마토를 익혔으며, 이때 날짜를 업데이트 해주었다. | ||
''' | ||
|
||
import sys | ||
from collections import deque | ||
|
||
m, n = map(int, sys.stdin.readline().split()) | ||
tomato = [] | ||
|
||
for i in range(n): | ||
tomato.append(list(map(int, sys.stdin.readline().split()))) | ||
|
||
queue = deque([]) | ||
dx = [0, 0, -1, 1] | ||
dy = [1, -1, 0, 0] | ||
|
||
for i in range(n): | ||
for j in range(m): | ||
if tomato[i][j] == 1: | ||
queue.append([i, j]) | ||
|
||
while queue: | ||
nx, ny = queue.popleft() | ||
|
||
for i in range(4): | ||
if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m: | ||
if tomato[nx + dx[i]][ny + dy[i]] == 0: | ||
queue.append([nx + dx[i], ny + dy[i]]) | ||
tomato[nx + dx[i]][ny + dy[i]] = tomato[nx][ny] + 1 | ||
|
||
result = 0 | ||
for i in range(n): | ||
for j in range(m): | ||
if tomato[i][j] == 0: | ||
print("-1") | ||
sys.exit() | ||
else: | ||
result = max(result, tomato[i][j]) | ||
|
||
print(result - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
''' | ||
[항체 인식](https://www.acmicpc.net/problem/22352) | ||
bfs를 만들어서 before에 맞춰서 after를 같이 탐색한다. | ||
이때 after의 수가 일관되지 않으면 -1을 반환하고, | ||
before와 after가 동일하면 0을 반환하고, | ||
before와 after가 다르면 1을 반환한다. | ||
-1은 백신 조건을 위배한 것으로 바로 No를 출력하고, | ||
0과 1은 계속 더해주면서 항체가 아예 변하지 않거나 한번만 변할 경우만 YES를 출력한다. | ||
''' | ||
|
||
|
||
import sys | ||
from collections import deque | ||
|
||
|
||
n, m = map(int, sys.stdin.readline().split()) | ||
before = [] | ||
after = [] | ||
visited = [[False for _ in range(m)] for _ in range(n)] | ||
|
||
for i in range(n): | ||
before.append(list(map(int, sys.stdin.readline().split()))) | ||
|
||
for i in range(n): | ||
after.append(list(map(int, sys.stdin.readline().split()))) | ||
|
||
|
||
def bfs(x, y): | ||
queue = deque([[x, y]]) | ||
data = before[x][y] | ||
update = after[x][y] | ||
dx = [0, 0, -1, 1] | ||
dy = [1, -1, 0, 0] | ||
possible = 0 | ||
|
||
if data != update: | ||
possible = 1 | ||
|
||
while queue: | ||
nx, ny = queue.popleft() | ||
visited[nx][ny] = True | ||
if after[nx][ny] != update: | ||
possible = -1 | ||
|
||
for i in range(4): | ||
if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m: | ||
if not visited[nx + dx[i]][ny + dy[i]] and before[nx + dx[i]][ny + dy[i]] == data: | ||
queue.append([nx + dx[i], ny + dy[i]]) | ||
|
||
return possible | ||
|
||
|
||
result = 0 | ||
for i in range(n): | ||
for j in range(m): | ||
if not visited[i][j]: | ||
possible = bfs(i, j) | ||
if possible == -1: | ||
print("NO") | ||
sys.exit() | ||
else: | ||
result += bfs(i, j) | ||
|
||
if result <= 1 : | ||
print("YES") | ||
else: | ||
print("NO") |