PAT 1003 我要通过! (20 分)

17 篇文章 0 订阅

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

我的代码,扣了两分,有知道的朋友解答一下

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


bool judge(string focus)
{
    int n;
    int counter_A=0;
    int counter_P=0;
    int counter_T=0;
    int counter_fir=0;
    int counter_sec=0;
    int counter_thir=0;
    char it;
    while(!focus.empty())
    {
        it=focus[0];
        focus.erase(0,1);
        if(it=='A')
        {
            if(counter_P==0&&counter_T==0)
            {
                counter_fir++;
            }
            else if(counter_P==1&&counter_T==0)
            {
                counter_sec++;
            }
            else if(counter_P==1&&counter_T==1)
            {
                counter_thir++;
            }
            else
            {
                return false;
            }
        }
        else if(it=='P'&&counter_T==0)
        {
            counter_P++;
        }
        else if(it=='T'&&counter_P==1)
        {
            counter_T++;
        }
        else
        {
            return false;
        }
    }
    if(counter_sec==0)
    {
        return false;
    }
    else if(counter_sec==1)
    {
        if(counter_fir!=counter_thir)
        {
            return false;
        }
    }
    else
    {
        if(counter_fir==0&&counter_thir==0)
        {
            return true;
        }
        //else if(counter_fir!=(counter_thir-counter_fir*(counter_sec-1)))
        else if(counter_fir*counter_sec!=counter_thir)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int n;
    string rec[10000];
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>rec[i];

    }
    for(int i=0; i<n; i++)
    {
    if(judge(rec[i]))
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;
    }
    }


}

 网上找了一个大佬的代码,22行完成了我100行代码的任务,还是满分,他用的C++11中的map结构,解题的思路也相当清晰。

1003. 我要通过!(20)-浙大PAT乙级真题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值