以下是我的代码,实在是看不出了,居然卡在测试0了
n=int(input())
str1='PAT'
flag=1
for i in range(n):
ans=input()
for j in range(len(ans)):
if(ans[j] not in str1):
flag=0
break
if flag:
if ans.count('P',0,len(ans)) != 1 or ans.count('T',0,len(ans)) != 1:
print('NO')
continue
a=ans.find('P',0,len(ans))
b=ans.find('T',0,len(ans))
if(b-a>1):
c=ans.count('A',0,a)
d=ans.count('A',a,b)
e=ans.count('A',b,len(ans))
if e == c*d:
print('YES')
else:
print('NO')
else:
print('NO')
else:
print('NO')
#解决了break失效的疑问!
#提供了’一次性输入多个数据,以Enter键为间隔‘的方法
a=int(input())
aList=[]
for j in range(0,a):
aList.append(input()) #一次性输入多个数据,以Enter键为间隔
def judge1(x): #字符串中只含有 P A T
for j in x:
if j!='P' and j!='A' and j!='T': #字符串可以直接进行比较,不需要转化成数字
s=0
#return 0 这里不能用return! 因为return语句会直接给出返回值,不执行break
break #终于明白了!break用在return后面会失效,不能跳出循环,因为此时函数judge1直接得到返回值,整个函数judge1结束了
else:
s=1
return s
def judge2(x): #字符串中只有一个P,一个T,至少一个A,且P在T前面
if judge1(x)==1:
xlist=list(x)
if xlist.count('P')==1 and xlist.count('T')==1 and xlist.index('P')<xlist.index('T'):
xmid = xlist[xlist.index('P'):xlist.index('T')]
if xmid.count('A')>=1:
return 1
else:
return 0
else:
return 0
else:
return 0
def judge3(x): #最终判定,这里难点在于对于题目本身的理解上,如何将题目要求转化成数学式,通过数学式来判断
if judge2(x)==1:
xlist=list(x)
xmid=xlist[xlist.index('P'):xlist.index('T')]
xleft=xlist[0:xlist.index('P')]
xright=xlist[xlist.index('T')+1:]
if xleft.count('A')*xmid.count('A')==xright.count('A'):
return 'YES'
else:
return 'NO'
else:
return 'NO'
for j in range(0,a):
print(judge3(aList[j]))