1
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] ABC 447(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

1
Posted at

[ABC447] ABC 447(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

A問題

  • 切り上げで割って考える.
A.py
"""
<方針>
- 切り上げで割って考える.
"""
# 入力
N, M = map(int, input().split())

# 出力
if((N+1)//2 >= M):
  print("Yes")
else:
  print("No")

B問題

  • Counter で最初に文字をカウントする.
  • 最後に構築する.
B.py
"""
<方針>
- `Counter` で最初に文字をカウントする.
- 最後に構築する.
"""
from collections import Counter
# 入力
S = list(input())

# 文字カウント
di = Counter(S)

# 最大値を取得
ma = max(di.values())

# 答えを構築
ans = ""
for s in S:
  # 最大値じゃない時,
  if(di[s] < ma):
    ans += s

# 出力
print(ans)

C問題

方針

  • A を除いて,文字列が一致すればいける.
  • あとは,合わさらない部分(多い・少ない)をカウントすれば良い.

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- `A` を除いて,文字列が一致すればいける.
- あとは,合わさらない部分(多い・少ない)をカウントすれば良い.
"""
# 入力
S = ["@"] + list(input()) + ["@"]
T = ["@"] + list(input()) + ["@"]

# 回数
ans = 0

iS = 0 # Sのindex
iT = 0 # Tのindex
while iS < len(S):
  # Aの個数をカウントする.
  aS = 0
  while iS < len(S):
    s = S[iS]
    if(s == "A"):
      aS += 1
    else:
      break
    iS += 1
  
  # tの個数を数える
  aT = 0
  while iT < len(T):
    t = T[iT]
    if(t == "A"):
      aT += 1
    elif(t == s):
      break
    else:
      # 文字列が一致しなさそう.
      print(-1)
      exit()
    iT += 1
  
  # Aの個数の差分を入れる
  ans += abs(aS-aT)
  
  iS += 1
  iT += 1

# 出力
print(ans)

補足

関係するリンク(参考文献など)

筆者について

その他

  • 間違いを含んでいる可能性があります.
  • 方針と言いつつ,方針ではない感想などを書いている可能性があります.
  • A問題から解説がだんだん省略されます.
  • コードに書かれている解説の文言と,その上に書いてある解説の文言を変えている場合があります.

最後に一言

  • あばばばばばばば
1
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
1
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?