백준 27

[백준/파이썬] 15651번 N과 M(3)

https://www.acmicpc.net/problem/15651 15651번: N과 M (3) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net import sys read = sys.stdin.readline def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): s.append(i) dfs() s.pop() n, m = map(int, read().split()) s = [] dfs() 중복도 허용되므로 visited 관련된 코드를 제거..

Algorithm/Problems 2023.02.01

[백준/파이썬] 15650번 N과 M (2)

https://www.acmicpc.net/problem/15650 15650번: N과 M (2) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net import sys read = sys.stdin.readline def dfs(): if (len(s) == m) and s == sorted(s): print(' '.join(map(str,s))) return for i in range(1, n+1): if visited[i]: continue visited[i] = True s.append(i) dfs() s.pop() visited[i]..

Algorithm/Problems 2023.02.01

[백준/파이썬] 15649번 N과 M (1)

https://www.acmicpc.net/problem/15649 15649번: N과 M (1) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net import sys read = sys.stdin.readline def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): if visited[i]: continue visited[i] = True s.append(i) dfs() s.pop() visited[i] = False n, m = map(i..

Algorithm/Problems 2023.02.01

[백준/파이썬] 1012번 유기농 배추

https://www.acmicpc.net/problem/1012 1012번: 유기농 배추 차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 www.acmicpc.net from collections import deque import sys read = sys.stdin.readline dx = [-1,1,0,0] dy = [0,0,-1,1] def bfs(x,y): queue = deque() queue.append((x,y)) matrix[x][y] = 0 while queue: a, b = queue.popleft() for i in range(4): nx = a + ..

Algorithm/Problems 2022.12.06

[백준/파이썬] 11478번 서로 다른 부분 문자열의 개수

https://www.acmicpc.net/problem/11478 11478번: 서로 다른 부분 문자열의 개수 첫째 줄에 문자열 S가 주어진다. S는 알파벳 소문자로만 이루어져 있고, 길이는 1,000 이하이다. www.acmicpc.net import sys read = sys.stdin.readline str = read().rstrip() subs = set() for i in range(len(str)): for j in range(i, len(str)): tmp = str[i:j+1] subs.add(tmp) print(len(subs)) 부분문자열로 한자리 문자부터 입력받은 문자열 길이까지의 문자로 이루어진 경우 (중복제외)를 구해야 한다. 이중 for문을 이용해 집합(set)으로 선언한 s..

Algorithm/Problems 2022.12.06

[백준/파이썬] 10816번 숫자 카드2

https://www.acmicpc.net/problem/10816 10816번: 숫자 카드 2 첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10, www.acmicpc.net import sys read = sys.stdin.readline n = int(input()) cards = list(map(int, read().split())) m = int(input()) nums = list(map(int, read().split())) count = {} for card in cards: if card in count: count[ca..

Algorithm/Problems 2022.11.18

[백준/파이썬] 1620번 나는야 포켓몬 마스터 이다솜

https://www.acmicpc.net/problem/1620 1620번: 나는야 포켓몬 마스터 이다솜 첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면 www.acmicpc.net import sys read = sys.stdin.readline n,m = list(map(int, read().split())) poke_dict = {} q = [] idx = 1 for _ in range(n): key = idx val = read().rstrip() poke_dict[key] = val idx += 1 q = [read().rstrip() f..

Algorithm/Problems 2022.11.18

[백준/파이썬] 14425번 문자열 집합

https://www.acmicpc.net/problem/14425 14425번: 문자열 집합 첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다. 다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어 www.acmicpc.net import sys read = sys.stdin.readline n,m = list(map(int, read().split())) cnt = 0 set_input = set([read() for _ in range(n)]) for _ in range(m): com_input = read() if com_input in set_input: cnt += 1 print..

Algorithm/Problems 2022.11.18

[백준/파이썬] 10815번 숫자 카드

https://www.acmicpc.net/problem/10815 10815번: 숫자 카드 첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10, www.acmicpc.net import sys read = sys.stdin.readline n = int(input()) cards = list(map(int, read().split())) m = int(input()) nums = list(map(int, read().split())) result = [0] * len(nums) for i in range(m): if nums[i] in ..

Algorithm/Problems 2022.11.18

[백준/파이썬] 1436번 영화감독 숌

https://www.acmicpc.net/problem/1436 1436번: 영화감독 숌 666은 종말을 나타내는 숫자라고 한다. 따라서, 많은 블록버스터 영화에서는 666이 들어간 제목을 많이 사용한다. 영화감독 숌은 세상의 종말 이라는 시리즈 영화의 감독이다. 조지 루카스는 스타 www.acmicpc.net import sys read = sys.stdin.readline n = int(read().rstrip()) nth = 666 # 비교할 숫자 cnt = 0 while True: if '666' in str(nth): cnt += 1 if cnt == n: print(nth) break nth += 1 처음에는 for문을 써야하나 복잡하게만 생각을 했다. 하지만, 의외로 간단한 문제였다. 66..

Algorithm/Problems 2022.11.17