Love丶FFC's Blog

PAT乙级:1003 我要通过!

2020-01-25 10:39:35
阅读:518   •   评论:6
标签:,

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

得到“答案正确”的条件是:

  1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 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

解题思想:

1.首先判断条件1

2.对条件2和条件3进行合并判断

3.条件3是递归定义,但是也有简单的规律可循

4.根据条件3的定义,发现条件3的规律如下:

xPATx

xPAATxx

xPAAATxxx

xPAAAATxxxx

xPAAAAATxxxxx

······

可以发现P与T之间A的个数与T右边x的个数一致,而P左边的x永远只有一个

因此x*(P与T之间A的个数)=T右边x的个数

在Python中,可以对字符串进行乘法运算,3*'A'="AAA"

时间复杂度:最好O(n),最坏O(n^2)

代码如下:

  1. NOS = int(input())
  2. List = list()
  3. for i in range(NOS):
  4. Judge = True # 条件1标记
  5. String = input()
  6. if 'P' in String and 'A' in String and 'T' in String: # 判断是否有P、A、T
  7. for j in range(len(String)):
  8. if String[j] != 'P' and String[j] != 'A' and String[j] != 'T': # 判断是否仅有P、A、T
  9. Judge = False
  10. break
  11. else:
  12. Judge = False
  13. if Judge == True: # 满足条件1
  14. a = String[:String.find('P')] # 截取P左边的字符串
  15. b = String[String.find('P') + 1:String.rfind('T')] # 截取P与T之间的字符串
  16. c = String[String.rfind('T') + 1:] # 截取T右边的字符串
  17. if (a == c and b == 'A') or (len(b) * a == c): # 满足条件2或条件3
  18. print("YES")
  19. else: # 不满足条件2和条件3
  20. print("NO")
  21. else: # 不满足条件1
  22. print("NO")

评论板

共有 6 条评论

  1. cheap jordans online

    Read reviews and was a little hesitant since I had already inputted my order. as well as but thank god, I had no issues. appreciate the received item in a timely matter, they are in new condition. situation so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans online https://www.realjordansshoes.com/

  2. authentic cheap jordans

    Read reviews and was a little hesitant since I had already inputted my order. as well but thank god, I had no issues. simillar to the received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
    authentic cheap jordans https://www.realcheapretrojordanshoes.com/

  3. cheap louis vuitton outlet

    Read reviews and was a little hesitant since I had already inputted my order. or maybe but thank god, I had no issues. such as the received item in a timely matter, they are in new condition. you ultimately choose so happy I made the purchase. Will be definitely be purchasing again.
    cheap louis vuitton outlet https://www.louisvuittonsoutletonline.com/

  4. cheap real jordans

    Read reviews and was a little hesitant since I had already inputted my order. as well as but thank god, I had no issues. exactly like the received item in a timely matter, they are in new condition. regardless so happy I made the purchase. Will be definitely be purchasing again.
    cheap real jordans https://www.realjordansretro.com/

  5. Knitant

    To this end, we stably transfected CHO cells with ESAM or JAM A and isolated cell clones expressing either ESAM or JAM A at similar levels Figure 6E generic cialis online europe For me, until I see a doctor and get an ultrasound to size the baby up it gives you the most accurate due date I pick one of the days I could have possible conceived, during ovulation, and use that

  6. invoimi

    top 10 viagra tablets Bear in mind that during this era, smoking was very common, and secondhand smoke was not seen as a violation of a non smoker s clean air

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:449444