PAT乙级练习题1003 我要通过!

题目

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

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

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

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

输入格式:

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

输出格式:

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

输入样例:

10
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
APT
APATTAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO
NO
NO

分析:

  1. 三个条件是层层递进的,不是相互独立的
  2. x, a, b, c表示的是字符串或空字符串,由多个字符组成,不一定是单个字符
  3. 条件二,PAT前后的字符串A的数量必须相等
  4. 条件三,如果a和c是空字符串,结合条件一和条件二,b只能为A,那么PAT成立,则推出PAAT成立,以此类推;
  5. 条件三,如果a,c不是空字符串,结合条件二,设AAPATAA成立,则AAPAATAAAA成立,此时a为AA,b为AA,c为AAAA,则AAPAAATAAAAAA成立,以此类推,得T之后A的数量 = P之前A的数量 × P&T中间A的数量
  6. 综上,P和T只能出现一次,并且按顺序出现;P和T之间必须有A,且A的数量符合条件二和条件三

答案

#include <iostream>
#include <cstring>
using namespace std;

int main(){
    int n;
    char test[100];
    
    cin >> n;
    
    for(int i = 0; i < n; ++i){
        int P_count = 0;
        int A_count = 0;
        int T_count = 0;
        int X_count = 0;
        int P_index = -1;
        int T_index = -1;
            
        
        cin >> test;
        for(int j = 0; j < strlen(test); ++j){
            
            if(test[j] == 'P'){
                P_count++;
                P_index = j;
            }
            else if(test[j] == 'A'){
                A_count++;
            }
            else if(test[j] == 'T'){
                T_count++;
                T_index = j;
            }
            else{
                X_count++;
            }
        }
        
        if(P_count != 1 || T_count != 1 || X_count != 0 || (T_index - P_index) <= 1 || P_index * (T_index - P_index - 1) != (strlen(test) - T_index - 1)){
            cout << "NO" << endl;
        }
        else{
            cout << "YES" << endl;
        }
    }
    
    return 0;
    
}

踩坑答案

#include <iostream>
#include <cstring>
using namespace std;

int main(){
    int n;
    char test[100];
    
    cin >> n;
    
    for(int i = 0; i < n; ++i){
        int P_count = 0;
        int A_count = 0;
        int T_count = 0;
        int X_count = 0;
        int P_index = -1;
        int T_index = -1;
            
        
        cin >> test;
        for(int j = 0; j < strlen(test); ++j){
            
            if(test[j] == 'P'){
                P_count++;
                P_index = j;
            }
            else if(test[j] == 'A'){
                A_count++;
            }
            else if(test[j] == 'T'){
                T_count++;
                T_index = j;
            }
            else{
                X_count++;
            }
        }
        
        if(P_count != 1 || T_count != 1 || X_count != 0){
            cout << test << endl;
            cout << "NO" << endl;
            continue;
        }
        if(P_index == 0 && T_index == (strlen(test) - 1) && A_count != 0){
            
            cout << "YES" << endl;
            continue;
        }
        //本来想靠A的数量建立关系式的,但是不同位置A的数量不对也可以通过此if校验,比如PT,当T_index - P_index - 1 = 0就会出现问题
        if(P_index + (T_index - P_index - 1) + P_index * (T_index - P_index - 1) == A_count){
            cout << test << endl;
            cout << "YES" << endl;
            continue;
        }
        cout << test << endl;
    }
    
    return 0;
    
}

我觉得很优秀的答案

#include<stdio.h>
int main()
{
    char c;
    int num;
    scanf("%d", &num);
    while(getchar() != '\n');  //来清除scanf缓存中的垃圾数据
    int i=0;//count表示A分别在几个位置 的数量
    for(i=0;i<num;i++)
    {
        int pos=0,count[3]={0,0,0};//,count[3]={0,0,0}不能放上面
        while((c=getchar())!='\n'){
            if(c=='A') count[pos]++;
            else if(c=='P'&&pos==0) pos=1;
            else if(c=='T'&&pos==1) pos=2;
            else break;
        }        
        if(c=='\n'&&
            pos==2&&
            count[1]&&
            count[2]==count[1]*count[0])
            printf("YES\n");        //也可以用puts("YES");自带换行符
        else  
            printf("NO\n");        //也可以用puts("YES");自带换行符
        if(c!='\n')
            while(getchar()!='\n');   //来清除getchar缓存中的垃圾数据
    }    
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值