結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー rlangevinrlangevin
提出日時 2023-10-03 20:09:25
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,489 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 142,880 KB
最終ジャッジ日時 2023-10-03 20:09:34
合計ジャッジ時間 8,332 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 988 ms
86,224 KB
testcase_01 AC 841 ms
83,456 KB
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 AC 96 ms
76,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SegmentTree:
    def __init__(self, size, f=min, default=10):
        self.size = 2**(size-1).bit_length() 
        self.default = default
        self.dat = [default]*(self.size*2) 
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) 
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res

M = int(input())
for _ in range(M):
    S = list(input())
    N = len(S)
    T = SegmentTree(N)
    problem = "problem"
    for i in range(N - 7):
        cnt = 0
        for j in range(7):
            cnt += int(S[i + j] != problem[j])
        T.update(i, cnt)
        
    ans = 100
    good = "good"
    problem_cnt = 0
    for i in range(N - 4):
        cnt = problem_cnt
        for j in range(4):
            cnt += int(S[i + j] != good[j])
        ans = min(ans, cnt + T.query(i + 4, N))
        if i >= 6:
            if S[i-6:i+1] == list(problem):
                problem_cnt += 1
                
    print(ans)            
0