1003 我要通过 (20 分) python

def find_P_T(s):
    cnt_p = 0  # P的个数
    cnt_t = 0  # T的个数
    loc_p = 0  # P的位置
    loc_t = 0  # T的位置
    cnt = -1  # 计数
    flag = 1  # 是否有非法字符
    for char in string:
        cnt += 1
        if char == 'P' and cnt_p == 0:
            cnt_p += 1
            loc_p = cnt  # 记录P的位置
            continue
        if char == 'T' and cnt_p == 1:
            cnt_t += 1
            loc_t = cnt  # 记录T的位置
            continue
        if char != 'A':
            flag = 0
            break
    if cnt_t * cnt_p != 1 or loc_t - loc_p == 1:
        flag = 0
    return flag, loc_p, loc_t


a_res = []  # 保留每一次检验的结果
n = int(input())
while n:
    n -= 1
    string = input()
    res = find_P_T(string)
    if res[0]:  # 不包含非法字符
        if res[1] * (res[2] - res[1] - 1) == len(string) - res[2] - 1:
            a_res.append('YES')
        else:
            a_res.append('NO')
    else:
        a_res.append('NO')
for i in a_res:
    print(i)


猜你喜欢

转载自blog.csdn.net/m0_50609661/article/details/122499908