ACwing 4261. 孤独的照片
错误题解
# acwing 4261. 孤独的照片
# 输入数字和字符串
lenth=int(input())
strr=input()
# 遍历count计数
count=0
for starts in range(0,lenth-2):
for endlen in range(3,lenth-starts+1):
if strr.count('G',starts,starts+endlen)==1 or strr.count('H',starts,starts+endlen)==1:
count+=1
print(count)
选择使用python报错
代码提交状态: Runtime Error
Traceback (most recent call last):
File "a.py", line 5, in <module>
strr=input()
File "<string>", line 1, in <module>
NameError: global name 'GHGHG' is not defined
版本问题,这是python3的语法,但是选的版本不是python3
使用python3
代码提交状态: Time Limit Exceeded
代码运行状态: 错误数据如下所示
输入
5000
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGHGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG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GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGHGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG...
输出
标准答案
1025886
正确题解
# acwing 4261. 孤独的照片
lenth=int(input())
strs=input()
li=[]
h=0
g=0
for i in range(0,lenth):
if strs[i]=='G':
li.append(h)
h=0
g+=1
else:
li.append(g)
h+=1
g=0
h=0
g=0
ri=[0 for i in range(0,lenth)]
for i in range(lenth-1,-1,-1):
if strs[i]=='G':
ri[i]=h
h=0
g+=1
else:
ri[i]=g
h+=1
g=0
count=0
for i in range(0,lenth):
count+=ri[i]*li[i]+max(ri[i]-1,0)+max(li[i]-1,0)
print(count)
ps: 由于ri是从后面填到前面的,使用insert填充的话会出现超时,应该是python写的那个函数使用的是一个动态数组,插入一个值,就得把已有的值一个一个挪到后面,所以就出现了超时(不过只是猜想)