1. Problem
2. My Solution
import sys
def permutation(level):
if level >= m:
if arr not in res:
res.append(arr.copy())
else:
for i in range(n):
if visited[i] == True:
continue
else:
arr[level] = nums[i]
visited[i] = True
permutation(level+1)
visited[i] = False
n, m = map(int,sys.stdin.readline().rstrip().split())
nums = sorted(list(map(int,sys.stdin.readline().rstrip().split())))
arr = [0] * m
visited = [False] * n
res = []
permutation(0)
for i in res:
print(' '.join(map(str,i)))
import sys
from itertools import permutations
n, m = map(int,sys.stdin.readline().rstrip().split())
nums = sorted(list(map(int,sys.stdin.readline().rstrip().split())))
for i in sorted(set(permutations(nums,m))):
print(*i)
import sys
def permutation(level):
if level >= m:
res.add(tuple(arr))
else:
for i in range(n):
if visited[i] == True:
continue
else:
arr[level] = nums[i]
visited[i] = True
permutation(level+1)
visited[i] = False
n, m = map(int,sys.stdin.readline().rstrip().split())
nums = sorted(list(map(int,sys.stdin.readline().rstrip().split())))
arr = [0] * m
visited = [False] * n
res = set()
permutation(0)
for i in sorted(res):
print(*i)
3. Learned
1. Problem
2. My Solution
import sys
from itertools import combinations
l, c = map(int,sys.stdin.readline().rstrip().split())
alphabet = sorted(list(sys.stdin.readline().rstrip().split()))
for i in combinations(alphabet,l):
count = 0
for a in ['a','e','i','o','u']:
if a in i:
count += 1
if l - count >= 2 and count > 0:
print(*i,sep='')
import sys
def permutation(level, pre):
if level >= l:
count = 0
for i in arr:
if i in 'aeiou':
count += 1
if l - count >= 2 and count > 0:
res.append(arr.copy())
else:
for i in range(c):
if visited[i] == True:
continue
else:
if str(pre) < alphabet[i]:
arr[level] = alphabet[i]
visited[i] = True
permutation(level+1, alphabet[i])
visited[i] = False
l, c = map(int,sys.stdin.readline().rstrip().split())
alphabet = sorted(list(sys.stdin.readline().rstrip().split()))
arr = [''] * l
visited = [False] * c
res = []
permutation(0,0)
for i in res:
print(*i, sep='')
3. Others' Solutions
import sys
def permutation(level, index):
if level >= l:
count = 0
for i in arr:
if i in 'aeiou':
count += 1
if l - count >= 2 and count > 0:
res.append(arr.copy())
else:
for i in range(index, c):
if visited[i] == True:
continue
else:
arr[level] = alphabet[i]
visited[i] = True
permutation(level+1, i+1)
visited[i] = False
l, c = map(int,sys.stdin.readline().rstrip().split())
alphabet = sorted(list(sys.stdin.readline().rstrip().split()))
arr = [''] * l
visited = [False] * c
res = []
permutation(0,0)
for i in res:
print(*i, sep='')
4. Learned
1. Problem
2. My Solution
import sys
n = int(sys.stdin.readline())
arr = set()
for _ in range(n):
op = sys.stdin.readline().rstrip().split()
if op[0] == 'add':
arr.add(op[1])
elif op[0] == 'remove':
arr.discard(op[1])
elif op[0] == 'check':
if op[1] in arr:
print(1)
else:
print(0)
elif op[0] == 'toggle':
if op[1] in arr:
arr.discard(op[1])
else:
arr.add(op[1])
elif op[0] == 'all':
arr = set(['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20'])
else: # empty
arr = set()
3. Others' Solutions
import sys
n = int(sys.stdin.readline())
bit = 0
for _ in range(n):
op = sys.stdin.readline().rstrip().split()
if op[0] == 'add':
bit |= (1 << int(op[1]))
elif op[0] == 'remove':
bit &= ~(1 << int(op[1]))
elif op[0] == 'check':
if bit & (1 << int(op[1])) == 0:
print(0)
else:
print(1)
elif op[0] == 'toggle':
bit ^= (1 << int(op[1]))
elif op[0] == 'all':
bit = (1 << 21) -1
else: # empty
bit = 0
4. Learned