結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー gew1fw
提出日時 2025-06-12 17:06:35
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,915 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 83,440 KB
最終ジャッジ日時 2025-06-12 17:06:43
合計ジャッジ時間 7,785 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    T = int(data[0])
    cases = data[1:T+1]
    
    for S in cases:
        len_S = len(S)
        cost_good = [0] * (len_S - 3)
        for i in range(len_S - 3):
            cost = 0
            for k in range(4):
                if S[i + k] != "good"[k]:
                    cost += 1
            cost_good[i] = cost
        
        cost_problem = [0] * (len_S - 6)
        for j in range(len_S - 6):
            cost = 0
            for k in range(7):
                if S[j + k] != "problem"[k]:
                    cost += 1
            cost_problem[j] = cost
        
        is_problem = [False] * (len_S - 6)
        for j in range(len_S - 6):
            if S[j:j+7] == "problem":
                is_problem[j] = True
        
        prefix_sum = [0] * (len_S + 1)
        for i in range(1, len_S + 1):
            prefix_sum[i] = prefix_sum[i - 1]
            if i - 1 < len(is_problem) and is_problem[i - 1]:
                prefix_sum[i] += 1
        
        earliest_good = None
        for i in range(len_S - 3):
            if S[i:i+4] == "good":
                earliest_good = i
                break
        
        earliest_problem = None
        for j in range(len_S - 6):
            if S[j:j+7] == "problem":
                earliest_problem = j
                break
        
        if earliest_good is not None and earliest_problem is not None and earliest_good < earliest_problem:
            print(0)
            continue
        
        min_total = float('inf')
        for i in range(len_S - 3):
            for j in range(i + 4, len_S - 6):
                total = cost_good[i] + cost_problem[j] + prefix_sum[i]
                if total < min_total:
                    min_total = total
        
        print(min_total if min_total != float('inf') else 0)

if __name__ == "__main__":
    main()
0