PAT 1003.我要通过1

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

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

  1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入格式:

每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。

输出格式:

每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES,否则输出 NO

输入样例:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO
  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Text.RegularExpressions;
  7 using System.Threading.Tasks;
  8 
  9 namespace ConsoleApp5
 10 {
 11     class Program
 12     {
 13         static void Main(string[] args)
 14         {
 15             /*字符串中只能含有PAT,1个P,1个T,多个A,且P一定在T前,PT直接至少一个A
 16              * xPATx是正确的,(x为空串或者A)
 17              * 条件3基于条件2,若aPbTc is right,那么aPAbTca 也是right
 18              * 假设A(n),B(n),C(n),为n次递归后含有A的个数
 19              * 基于条件2
 20              * 根据条件得A(1)=C(1),B(1)=1
 21              * 根据条件3
 22              * A(n)=A(1),B(n)=B(n-1)+1,C(n)=C(n-1)+A(1)
 23              * 解的
 24              * A(n)=A(1)
 25              * B(n)=B(1)+(n-1)=1+n-1=n
 26              * C(n)=C(1)+(n-1)*A(1)=n*A(1)
 27              * =>A(n)*B(n)=C(n)
 28              */
 29             
 30             int count = 10;
 31             //输入一个数值,表示要测试多少个用例
 32             count = Convert.ToInt32(Console.ReadLine());
 33             //存放输入的用例
 34             string[] strIn = new string[count];
 35             //存放YES or NO
 36             string[] strOut = new string[count];
 37             //输入用例并存放在strIn数组中
 38             for (int i = 0; i < count; i++)
 39             {
 40                 strIn[i] = Console.ReadLine();
 41             }
 42 
 43             //对用例进行判断
 44             for (int i = 0; i < count; i++)
 45             {
 46                 //获取用例中"P","T"的个数
 47                 int pnumber = Regex.Matches(strIn[i], "P").Count;
 48                 int tnumber = Regex.Matches(strIn[i], "T").Count;
 49                 //获取用例中"P""T"的下标
 50                 int indexP = strIn[i].IndexOf("P");
 51                 int indexT = strIn[i].IndexOf("T");
 52                 //判断用例是否只含有PAT三种字符
 53                 for (int j = 0; j < strIn[i].Length; j++)
 54                 {
 55                     if (strIn[i][j]!='A'&& strIn[i][j] != 'P' && strIn[i][j] != 'T' )
 56                     {
 57                         strOut[i] = "NO";
 58                     }
 59                     //筛选出只含有PAT字符的字符串
 60                     else
 61                     {
 62                         //筛选出P,T个数只为1的字符串
 63                         if (pnumber == 1 && tnumber == 1)
 64                         {
 65                             //P在A前面
 66                             if (indexP < indexT)
 67                             {
 68                                 //获取lengtha,lengthb,lengthc,表示A(n),B(n),C(n)
 69                                 int lengtha, lengthb, lengthc;
 70                                 string a, b, c;
 71                                 a = strIn[i].Substring(0, indexP + 1);
 72                                 lengtha = Regex.Matches(a, "A").Count;
 73 
 74                                 b = strIn[i].Substring(indexP + 1, indexT - indexP - 1);
 75                                 lengthb = Regex.Matches(b, "A").Count;
 76 
 77                                 c = strIn[i].Substring(indexT + 1);
 78                                 lengthc = Regex.Matches(c, "A").Count;
 79                                 //排除PT直接没有A的字符串
 80                                 if (lengthb==0)
 81                                 {
 82                                     strOut[i] = "NO";
 83                                     break;
 84                                     ;
 85                                 }
 86                                 //满足条件A(n)*B(n)=C(n)
 87                                 if (lengtha * lengthb == lengthc)
 88                                 {
 89                                     strOut[i] = "YES";
 90                                 }
 91                                 else
 92                                 {
 93 
 94                                     strOut[i] = "NO";
 95                                 }
 96 
 97                             }
 98                             else
 99                             {
100 
101                                 strOut[i] = "NO";
102                             }
103 
104                         }
105 
106                         else
107                         {
108                             strOut[i] = "NO";
109                         }
110                     }
111                 }
112                 
113 
114             }
115             //输出答案
116             for (int i = 0; i < count; i++)
117             {
118                     Console.WriteLine(strOut[i]);
119             }
120 
121             Console.ReadKey();
122             }
123         }
124 }

猜你喜欢

转载自www.cnblogs.com/jcahsy/p/11964698.html