1003 我要通过!(20)(20 分) “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。 得到“答案正确”的条件是: 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;\ 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;\ 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。 现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。 输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。 输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。 输入样例: 8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA 输出样例: YES YES YES YES NO NO NO NO ———————————————— Python写一段最优代码解决这个问题
时间: 2024-04-16 19:29:39 浏览: 24
def judgePAT(n, strings):
result = []
for i in range(n):
string = strings[i]
count_P = string.count('P')
count_A = string.count('A')
count_T = string.count('T')
count_other = len(string) - count_P - count_A - count_T
if count_P == 1 and count_A > 0 and count_T == 1 and count_other == 0:
index_P = string.index('P')
index_T = string.index('T')
x = string[:index_P]
y = string[index_P + 1:index_T]
z = string[index_T + 1:]
if len(y) > 0 and y.count('A') == len(y) and len(x) * len(y) == len(z):
result.append("YES")
else:
result.append("NO")
else:
result.append("NO")
return result
n = int(input())
strings = []
for i in range(n):
strings.append(input())
result = judgePAT(n, strings)
for res in result:
print(res)
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)