AtCoder Beginner Contest 386の解答等の速報的まとめ
A問題
2種類渡されているとき、条件に当てはまる
A
print("Yes" if len(set(input().split())) == 2 else "No")
B問題
00を0に置換して文字列を数える
B
s = input().replace("00", "0")
print(len(s))
C問題
まったく同じか変更する1箇所以外同じであるかを調べる
C
def same_string_count(s, t):
res = 0
for s_i, t_i in zip(s, t):
if s_i == t_i:
res += 1
else:
break
return res
k = int(input())
s = input()
t = input()
if s == t:
exit(print("Yes"))
if abs(len(s) - len(t)) >= 2:
exit(print("No"))
a = same_string_count(s, t)
b = same_string_count(s[::-1], t[::-1])
if (len(s) == len(t) and len(s) - a - b <= 1) or (len(s) != len(t) and a + b >= min(len(s), len(t))):
print("Yes")
else:
print("No")
D問題
縦横それぞれで
インデックス順に、白に塗らないといけない場所と黒に塗らないといけない場所の境界が次第に小さくなればいい
D
def check(d):
last_white = n + 1
for key in sorted(d.keys()):
white_flg = False
white_ind = n + 1
black_ind = -1
lst = sorted(d[key])
for ind, c in lst:
if c == "W":
white_flg = True
white_ind = min(white_ind, ind)
else:
if white_flg:
return False
black_ind = ind
if black_ind >= last_white:
return False
last_white = min(last_white, white_ind)
return True
n, m = map(int, input().split())
x = dict()
y = dict()
for _ in range(m):
x_i, y_i, c_i = input().split()
x_i = int(x_i)
y_i = int(y_i)
if x_i not in x:
x[x_i] = list()
if y_i not in y:
y[y_i] = list()
x[int(x_i)].append((int(y_i), c_i))
y[int(y_i)].append((int(x_i), c_i))
print("Yes" if check(x) and check(y) else "No")
E問題
combinationsで全組み合わせを出力してXORの最大値を求める
E
from itertools import combinations
n, k = map(int, input().split())
a = list(map(int, input().split()))
default = 0
if k * 2 > n:
for a_i in a:
default ^= a_i
k = n - k
ans = 0
for p in combinations(a, k):
ans_i = default
for p_i in p:
ans_i ^= p_i
ans = max(ans, ans_i)
print(ans)