#38755: 滑動視窗解法(求更短的解法)


s011388@fysh.tc.edu.tw (pollux)

學校 : 國立豐原高級中學
編號 : 189768
來源 : [180.217.119.157]
最後登入時間 :
2024-02-17 10:41:16
c462. apcs 交錯字串 (Alternating Strings) -- apcs | From: [111.252.237.6] | 發表日期 : 2023-12-21 20:31

k = int(input())
stg = list(map(lambda x:int(x.isupper()),input()))
ans = 0
#更換起始位置,尋找最優解
for i in range(len(stg)):
    ct = i
    bs = -1 #-1,0,1 代表未定義開頭、大寫、小寫
    tp = 0 #區域解
    while 1:
        #當滑動視窗到達盡頭就break
        if ct+k>len(stg):break
        #當滑動視窗滑到相同大小寫的子陣列
        if len(set(stg[ct:ct+k]))==1:
            #如果尚未找到開頭或是符合交錯規則
            if bs==-1 or (bs!=stg[ct] and bs != -1):
                bs=stg[ct];ct+=k;tp+=k;ans=max(ans,tp)
            else:break    
        else: break    
print(ans)  

 
ZeroJudge Forum