0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC447をPythonで

0
Posted at

AtCoder Beginner Contest 447の解答等の速報的まとめ

A問題

端数切り上げ

A
n, m = map(int, input().split())
print("Yes" if (n + 1) // 2 >= m else "No")

B問題

最大の個数を数える

B
s = input()

count = dict()
for s_i in s:
    if s_i not in count:
        count[s_i] = 0
    count[s_i] += 1

count_max = max(count.values())
ng = set()
for key, val in count.items():
    if count_max == val:
        ng.add(key)

ans = list()
for s_i in s:
    if s_i not in ng:
        ans.append(s_i)

print(*ans, sep="")

C問題

$A$を取った文字列が一致しているときのみ対象
各$A$以外の文字間にある$A$の個数の差が答え

C
def delete_a(S):
    res = list()
    a_list = [0]
    for s_i in S:
        if s_i != "A":
            res.append(s_i)
            a_list.append(0)
        else:
            a_list[-1] += 1

    return res, a_list


s = input()
t = input()

del_s, s_a = delete_a(s)
del_t, t_a = delete_a(t)

if del_s != del_t:
    exit(print(-1))

if len(s_a) < len(t_a):s_a.append(0)
if len(s_a) > len(t_a):t_a.append(0)

ans = 0
for s_i, t_i in zip(s_a, t_a):
    ans += abs(s_i - t_i)

print(ans)

D問題

左から順に$B$に到達したとき、

  • $B$の左にまだカウントしていない$A$
  • $B$の右にまだカウントしていない$C$

があるとき答えに1を足す

D
s = input()

a_count = 0
c_ind = 0
ans = 0
for i, s_i in enumerate(s):
    if s_i == "A":
        a_count += 1
    elif s_i == "B":
        c_ind = max(c_ind, i)
        while c_ind < len(s) and s[c_ind] != "C":
            c_ind += 1
        if c_ind < len(s) and s[c_ind] == "C" and a_count > 0:
            ans += 1
            a_count -= 1
            c_ind += 1

print(ans)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?