By 1003 I want to! (20 points)
"Correct answer" is the most joy automatic reply sentence topic given system. This title belongs to the PAT of "correct answer" big delivery - just read the string following conditions are satisfied, the system will output "correct answer", otherwise outputs "Wrong Answer."
Get the "correct answer" conditions are:
- String must have only P, A, T of the three characters, the characters may not contain other;
- The string of arbitrary shape can be obtained xPATx "correct answer", or where x is the empty string, or a string consisting only of letters A;
- If aPbTc is correct, then aPbATca is correct, wherein a, b, c or all the empty string, or a string consisting only of letters A.
Now ask you to write a PAT referee program automatically determines which strings can get "the answer right".
Input formats:
Each test comprises a test input. Line 1 is given a positive integer n (<10), is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.
Output formats:
The detection result row for each string, if the string can get "correct answer", the output YES, otherwise output NO.
Sample input:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
Sample output:
YES
YES
YES
YES
NO
NO
NO
NO
The key problem-solving: meet with all the elements and PAT , the determination is YESP前面的A的个数 乘以 P与T之间的A的个数 是否等于 T后面A的个数
AT > 0 and (AP*AT)==TA
AC Code:
def IsPat(s):
Pnum=s.count("P")
Anum=s.count("A")
Tnum=s.count("T")
if Pnum==1 and Tnum==1 and (Pnum+Anum+Tnum)==len(s):
AP=s.find("P") # 找到P的位置就相当于求得了P前面A的个数
T=s.find("T")
AT=T-AP-1
TA=len(s)-T-1
if AT > 0 and (AP*AT)==TA:
print("YES")
else:
print("NO")
else:
print("NO")
n =int(input())
for i in range(n):
s=input()
IsPat(s)
More than others a little perseverance, you will create a miracle