各位大大們求解
n = int(input())
s = input()
ch = []
big , small = 0 , 0
for i in s:
if i.isupper():
big += 1
if small != 0:
ch.append(small)
small = 0
else:
small += 1
if big != 0:
ch.append(big)
big = 0
if big != 0: ch.append(big)
if small != 0: ch.append(small)
#print(ch)
num = 0
maxs = 0
flag = False
for i in ch:
if i > n and num == 0:
num += n
elif i == n:
num += n
elif i > n:
num += n
flag = True
maxs = max([maxs , num])
if flag:
num = 0
flag = False
print(maxs)
各位大大們求解
n = int(input())
s = input()
ch = []
big , small = 0 , 0
for i in s:
if i.isupper():
big += 1
if small != 0:
ch.append(small)
small = 0
else:
small += 1
if big != 0:
ch.append(big)
big = 0
if big != 0: ch.append(big)
if small != 0: ch.append(small)#print(ch)
num = 0
maxs = 0
flag = False
for i in ch:
if i > n and num == 0:
num += n
elif i == n:
num += n
elif i > n:
num += n
flag = True
maxs = max([maxs , num])
if flag:
num = 0
flag = Falseprint(maxs)
問題在下面的for-loop
第一個是i<n時也要結算一次
第二個是可利用字串
在elif i>n:這行
要結算一次沒問題
但是接下來應該是從n開始算
例如2
aabbaaabbaabb
ch=[2,2,3,2,2,2]
答案是8(aabbaabb)aa是由aaa(3)獲得
所以可以開一個temp存取結算時可利用字串長度
上面一樣
AC code(for-loop)
...
elif i>n:
num+=n
temp=n
flag=True
else:
temp=0
flag=True
maxs=max([maxs,num])
if flag:
num=temp
flag=False