PAT B 1003 I want to pass!

The code was released, we learn together, help each other
Title:
Here Insert Picture Description
Input Sample:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

Analysis:
Eligible string must contain only P, A, T, and P T are mutually exclusive, and containing at least one of A, first rule out the string does not meet these conditions, then the problem can be obtained in accordance with the conditions "the answer right" into two categories:

1. xPATx

PAT A symmetrical on both sides of

   PAT
   APATA
   AAPATAA 
   AAAPATAAA
   ......

2. aPbTc correct -> aPbATca correct

B is only in the initial aPbTc A, a = c and to meet the correct conditions, here comprising two situations:

Empty string (1) .a, c as follows: the first on the left is P, the right side of the first T

   PAT
   PAAT
   PAAAT
   PAAAAT
   ......

(2) .a, c is a string consisting of A: T to P and is divided into three delimiter strings, satisfy: a first string length x = second string third string length length

   APATA               AAPATAA
   APAATAA             AAPAATAAAA            ......
   APAAATAAA           AAPAAATAAAAAA          
   1 x 3 = 3           2 x 3 = 6
   ......              ......                ......

Finally, the discussion points on the line, the following code (Python):

import re
pat = r'PA[A]*T'
for j in range(int(input())):
    flag = False  # 首先假设均不正确(False)
    x = input()
    cA = 0
    cP = 0
    cT = 0
    error = 0
    for i in x:
        if i == 'A':
            cA += 1
        elif i == 'T':
            cT += 1
        elif i == 'P':
            cP += 1
        else:
            error += 1
    if cA >= 1 and cP == 1 and cT == 1 and error == 0:  # 过滤掉不符合条件的字符串
        if re.findall(pat, x):  # 只匹配含'PA[A]*T'的字符串
            found = re.findall(pat, x)[0]
            if found == "PAT":  # 判断满足情况1 就True
                split = found.split('PAT')
                if split[0] == split[1]:
                    flag = True
            else:
                if x[0] == 'P' and x[-1] == 'T':  # 判断满足情况2.(1) 就True
                    flag = True
                else:
                    result = re.split(r'[P|T]', x)  # 以P,T为分隔符分割为三个字符串
                    if len(result[0]) * len(result[1]) == len(result[2]):  # 判断满足情况2.(2) 就True
                        flag = True
    if flag:  # 根据判断输出结果
        print('YES')
    else:
        print('NO')
Published 65 original articles · won praise 25 · views 1017

Guess you like

Origin blog.csdn.net/chongchujianghu3/article/details/105005626