PAT 乙级真题 1003 我要通过!题解

1003 我要通过! (20 分)

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

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

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

----------------------------------------------------------------------------------------------------------------------------------------------------------------------
PS:我怎么那么菜
由第二个条件可知
两边可以无限添加A
由第一个条件可知
PT中间一定要有A
且只能有PAT这三个字符
下面解释第三个条件
中间如果已经有了n个A
即b=n个A
下一个就会导致最后加上n个P前面的A数量(数学归纳法思想)
那么递推回去
n个中间的A
T后面的A一定会是P前面的A与P和T之间的A的乘积
下面上我注释完整
谁都能看懂的代码
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 //Author:LanceYu
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<fstream>
 7 #include<iosfwd>
 8 #include<sstream>
 9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 
33 int main()
34 {
35     char s[1001];
36     int n,sum[3];
37     cin>>n;
38     getchar();
39     while(n--)
40     {
41         int flag=0;
42         memset(sum,0,sizeof(sum));//初始化 
43         scanf("%s",s);
44         for(int i=0;i<strlen(s);i++)
45         {
46             if(s[i]=='A')
47                 sum[flag]++;
48             else if(s[i]=='P'&&flag==0)//如果第一次出现P的话 
49                 flag=1;
50             else if(s[i]=='T'&&flag==1)//如果T在P之后且T只出现一次的话 
51                 flag=2;
52             else
53             {
54                 flag=-1;//除此之外的情况返回-1 
55                 break;
56             }
57         }
58         if(flag!=-1&&(sum[1]*sum[0]==sum[2])&&sum[1])//满足条件的情况
59             cout<<"YES"<<endl;
60         else
61             cout<<"NO"<<endl;
62     }
63     return 0;
64 }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

结果还是Wrong Answer

第二个点死活过不了

后来发现真的是因为自己太菜

在最后的判定条件那里

如果P只出现了一次

没有出现T

这种情况就没有考虑

下面就更改了一下代码

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 1 //Author:LanceYu
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<fstream>
 7 #include<iosfwd>
 8 #include<sstream>
 9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 
33 int main()
34 {
35     char s[1001];
36     int n,sum[3];
37     cin>>n;
38     getchar();
39     while(n--)
40     {
41         int flag=0;
42         memset(sum,0,sizeof(sum));//初始化 
43         scanf("%s",s);
44         for(int i=0;i<strlen(s);i++)
45         {
46             if(s[i]=='A')
47                 sum[flag]++;
48             else if(s[i]=='P'&&flag==0)//如果第一次出现P的话 
49                 flag=1;
50             else if(s[i]=='T'&&flag==1)//如果T在P之后且T只出现一次的话 
51                 flag=2;
52             else
53             {
54                 flag=-1;//除此之外的情况返回-1 
55                 break;
56             }
57         }
58         if(flag==2&&(sum[1]*sum[0]==sum[2])&&sum[1])//满足条件的情况
59             cout<<"YES"<<endl;
60         else
61             cout<<"NO"<<endl;
62     }
63     return 0;
64 }

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------

成功AC

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Notes:博主是真的太菜了

2018-11-16  21:36:10  Author:LanceYu

原文地址:https://www.cnblogs.com/lanceyu/p/9971779.html