結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 997 ms
89,256 KB
testcase_01 AC 837 ms
85,932 KB
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 AC 99 ms
79,696 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