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?

AtCoder Beginner ContestをPythonで取り組んだ振り返り(ABC447 A~C)

0
Posted at

どうもこんにちは!

先週は所用で不参加、今週はスケジュールの都合で3完したところで打ち止めとしました。
Cまで振り返り。

問題と公式の解説のリンク

問題は以下のリンクから。

A - Seats 2 -

問題

N個の席が1直線上にあり、M人が隣り合わずに座れるかを判定する問題。

考え方とコード

座席が2M-1席以上あれば満たすと考えればよいです。例えば3人のときなら5席ですね。

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

B - mpp -

問題

文字列Sが与えられます。その中で出現回数が最も多い文字を並びはそのままでSからすべて取り除いた文字列を出力する問題。出現回数が最も多い文字が複数個ある場合はすべて取り除くとします。

考え方とコード

ひとまず文字列を読み込んで辞書を作って出現回数の最大値を把握します。その後文字列の文字を順番に見て出現回数が最大でない文字だけ保持していく形で文字列を作りました。

s = input()
d = {}
m = 0
for c in s:
    if c not in d:
        d[c] = 1
    else:
        d[c] += 1
    m = max(m,d[c])

ans = ''
for c in s:
    if d[c] != m:
        ans += c

print(ans)

C - Insert and Erase A -

問題

2つの文字列S,Tが与えられます。Sの文字列には以下の加工ができるとしたとき、SをTの文字列に一致させることができるか、できる場合は加工を最小で何回すればよいかを回答する問題。

  • Sの文字列の任意の位置にAを1個挿入する
  • Sの文字列内のAを1個削除し、以降の文字列を詰める

考え方とコード

ひとまずSかTの文字列の先頭からいずれかの文字列の最後まで、以下の走査をしていきます。

  • 文字が一致する場合:次の文字列の比較に移る
  • 文字が一致しておらずかつS側もT側もAではない場合:加工で一致しないとして終了
  • 文字が一致しておらずT側がAの場合:S側にAを追加するとして1回加工し、S側とT側は次の文字とで比較する
  • 文字が一致しておらずS側がAの場合:S側のAを連続する数だけ加工して削除し、S側は最後のAの次の文字でT側と比較する

上記の比較が終わった後、SまたはT側の走査が終わっていない場合は残っている文字がすべてAならその個数だけ加工回数にカウント、そうでなければ一致しないとして終了するとして最終的な加工回数を出力します。

s = input()
t = input()
ls, lt = len(s), len(t)
sidx, tidx = 0,0
ans = 0

while sidx < ls and tidx < lt:
    if s[sidx] == t[tidx]:
        sidx += 1
        tidx += 1
    elif s[sidx] != 'A' and t[tidx] != 'A' and s[sidx] != t[tidx]:
        ans = -1
        break
    elif t[tidx] == 'A':
        ans += 1
        tidx += 1
    elif s[sidx] == 'A':
        while sidx < ls and s[sidx] == 'A':
            ans += 1
            sidx += 1
        if sidx == ls or s[sidx] != t[tidx]:
            ans = -1
            break

if sidx == ls and tidx < lt:
    while tidx < lt:
        if t[tidx] != 'A':
            ans = -1
            break
        else:
            ans += 1
            tidx += 1
elif sidx < ls and tidx == lt:
    while sidx < ls:
        if s[sidx] != 'A':
            ans = -1
            break
        else:
            ans += 1
            sidx += 1
print(ans)

これでABCへの初参加から1年が経ちました。不参加は数回ありますがまずまず継続できたのはよかったかなと思います。
引き続きできるだけ参加していきたいと思います。

ではでは。

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?