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?

【Python】構造体の更新(paizaランク C 相当)の解法

Last updated at Posted at 2025-10-25

構造体の更新(paiza ランク C 相当)の回答例を作成しました。
問題から与えられる値の取得には splitTokensを使用しています。

splitTokens とは

splitTokens は単一行や複数行の半角/全角スペース、タブ、カンマ、改行が混在した文字列を配列に変換する関数です。問題の入力処理を省略することでアルゴリズムのプログラミングのみに集中できます。

Qiita:【Python】paizaや競プロの標準入力のプログラミング(splitTokens)
GitHub:splitTokens — 汎用入力パーサ

解答コード例

import sys,re

# main関数
def main():
    #(1)入力処理
    [n, _], *rest = splitTokens(sys.stdin.read())

    #(2)データ変換処理(配列→構造体(辞書型))
    members = [dict(zip(["nickname", "old", "birth", "state"], row)) for row in rest[:n]]

    #(3)データ更新処理(nicknameを更新)
    for a, nn in rest[n:]:
        members[a - 1]["nickname"] = nn

    #(4)出力処理
    for m in members:
        print(f'{m["nickname"]} {m["old"]} {m["birth"]} {m["state"]}')

#エントリーポイント
if __name__ == '__main__':
    main()

splitTokens
import sys,re

# 入力文字列を正規化
def normalizeInput(s: str) -> str:
    s = re.sub(r'\r\n?', '\n', s)       # 改行統一
    s = re.sub(r'[\t\u3000,]', ' ', s)  # 空白類(タブ・全角スペース・カンマ)を半角スペースに変換
    return s.strip()

# 文字型を数値型、小数型に変換
def convert_type(value):
    try:
        if '.' in value:
            return float(value)
        else:
            return int(value)
    except ValueError:
        return value
        
# 一行の文字列を一次元配列に変換
def parseSingleLine(s: str):
    return [convert_type(token) for token in s.split()]

# 複数行の文字列を二次元配列に変換
def parseMultiLine(s: str):
    return [parseSingleLine(line) for line in s.splitlines() if line.strip()]

# 変換関数
def splitTokens(input):
    normalized = normalizeInput(input)
    isSingleLine = '\n' not in normalized;
    # 単一行か複数行で処理を分岐
    return  parseSingl
ソースコード全体
import sys,re

# 入力文字列を正規化
def normalizeInput(s: str) -> str:
    s = re.sub(r'\r\n?', '\n', s)       # 改行統一
    s = re.sub(r'[\t\u3000,]', ' ', s)  # 空白類(タブ・全角スペース・カンマ)を半角スペースに変換
    return s.strip()

# 文字型を数値型、小数型に変換
def convert_type(value):
    try:
        if '.' in value:
            return float(value)
        else:
            return int(value)
    except ValueError:
        return value
        
# 一行の文字列を一次元配列に変換
def parseSingleLine(s: str):
    return [convert_type(token) for token in s.split()]

# 複数行の文字列を二次元配列に変換
def parseMultiLine(s: str):
    return [parseSingleLine(line) for line in s.splitlines() if line.strip()]

# 変換関数
def splitTokens(input):
    normalized = normalizeInput(input)
    isSingleLine = '\n' not in normalized;
    # 単一行か複数行で処理を分岐
    return  parseSingleLine(normalized) if isSingleLine else parseMultiLine(normalized);

# main関数
def main():
    #(1)入力処理
    [n, _], *rest = splitTokens(sys.stdin.read())

    #(2)データ変換処理(配列→構造体(辞書型))
    members = [dict(zip(["nickname", "old", "birth", "state"], row)) for row in rest[:n]]

    #(3)データ更新処理(nicknameを更新)
    for a, nn in rest[n:]:
        members[a - 1]["nickname"] = nn

    #(4)出力処理
    for m in members:
        print(f'{m["nickname"]} {m["old"]} {m["birth"]} {m["state"]}')

#エントリーポイント
if __name__ == '__main__':
    main()

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?