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?

AtCoder ABC394 振り返り(緑コーダーがPythonでABCD問題)

Posted at

ABC 394振り返り

鹿島建設プログラミングコンテスト2025(AtCoder Beginner Contest 394) - AtCoder

ABCD4問解答、レーティングは微減でした。

今回もD問題の難易度が低かった(Diff 253)ようで、水色問題解けない勢は問題を解くスピード勝負になってしまった模様。元々早解きは得意でない上に、途中でMacが固まってしまったアクシデントもあり、あまり良くない結果でした。

まあ、E問題(Diff 1403)を解ければよかったんですけどね...。Eは難しかったです。

A - 22222

2/22 開催なので、2を集める問題なのでしょうか。
入力文字列 S を調べて、もし S[i] == '2' なら答えに加えていく...という考えで解きました。

S = input()
answers = []
for i in range(len(S)):
    # 文字が '2' だったら、答えの配列に追加
    if S[i] == '2':
        answers.append(S[i])
# 配列を文字列にして出力
print("".join(answers))

B - cat

2/22 が猫の日なので、このタイトルなのでしょうか。
{key: 文字列の長さ, value: 文字列} のdictionary を作り、keyをsortすることで、文字列の長さ順で文字列を連結できるようにしました。

N = int(input()) 

# 入力文字列をdictionaryで保持
S = defaultdict(str)
for i in range(N):
    current_s = input()
    S[len(current_s)] = current_s

# keyをソート
sorted_key = list(S)
sorted_key.sort()

# keyの順番(=文字数順)で文字列を連結し、出力
answer = []
for key in sorted_key:
    answer.append(S[key])
print("".join(answer))

C - Debug

問題文・入力例を見ると、以下のように変更すれば良いことがわかる。

WA → AC
WWA → ACC
....
WWWWWWWWWWA → ACCCCCCCCCC

つまり、文字列 S の中に[Wの連続 + A] があれば、[A + Cの連続] に変換すれば良い。

S = list(input())

w_sequence = 0 # Wの連続個数
answer = []
for i in range(len(S)):
    # W だったらカウント
    if S[i] == "W":
        w_sequence += 1
        continue

    if w_sequence > 0 and S[i] == "A":
        # [Wの連続 + A] なら、[A + Cの連続] に変換
        start = i - w_sequence
        S[start] = "A"
        for j in range(start + 1, i + 1):
            S[j] = "C"
        w_sequence = 0
    
    if S[i] != "W":
        w_sequence = 0
        
print("".join(S))

D - Colorful Bracket Sequence

スタック (deque) を使用するとうまく解けます。

例として、入力例1を処理するときはこんな感じです。

1文字目: 空Stack ← ( # カッコが対応していないのでStackに追加
2文字目: ( ← [ # カッコが対応していないのでStackに追加
3文字目: ([ ← ] # []が対応しているので、一文字消去
4文字目: ( ← )  # () が対応しているので、一文字消去
...
from collections import deque

# 入力
S = list(input())

dq = deque() # 文字列を保持するスタック

# Sを一文字ずつ処理
for i in range(len(S)):
    # スタックの一番最後の一文字と、今の文字列が対応するカッコだったら、スタックから取り除く
    c = S[i]
    if len(dq) == 0:
        dq.append(c)
        continue
    if dq[-1] == "(" and c == ")":
        dq.pop()
        continue
    if dq[-1] == "[" and c == "]":
        dq.pop()
        continue
    if dq[-1] == "<" and c == ">":
        dq.pop()
        continue

    # カッコが対応していない場合、文字をスタックに追加
    dq.append(c)

# スタックが空ならYes
print("Yes" if len(dq) == 0 else "No")
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?