美文网首页
1003. 我要通过!

1003. 我要通过!

作者: Yovey | 来源:发表于2017-12-10 20:00 被阅读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

时间限制 内存限制 代码长度限制 判题程序 作者
400 ms 65536 kB 8000 B Standard CHEN, Yue

解题思路:
1、整理判断条件【仅有PAT】【形如xPATx】【 aPbTc==aPbATca】
分析可知,a*b=c(abc分别代表P前、PT间、T后的A数量)
2、没有特定要求的情况下,不用一口气输出,可以读一条输出一条,只要输出格式正确即可通过。
3、代码思路:
输入n;
循环->
输入字符串;
判断;
输出YES或者NO;
循环结束;

ACCode:

//I want to pass!
//Input:A intager n,and some Strings
//Output:YES or NO
#include <cstring>
#include <iostream>
using namespace std;
void PAT(char* str)
{//遍历字符串,获取P/T的位置,确认没有其他字母
    int j;
    int np = 0, nt = 0, other = 0, lp, lt;
    int len = strlen(str);
    for (j = 0; j<len; j++) {//记录P,T和其他字母的个数以及P和T的位置 
        if (str[j] == 'P') {
            np++;
            lp = j;
        }
        else if (str[j] == 'T') {
            nt++;
            lt = j;
        }
        else if (str[j] != 'A')
            other++;
    }
    if ((np != 1) || (nt != 1) || (other != 0) || (lt - lp <= 1))
    {//P和T的个数必须为一,没有其他字母,P和T中间至少有一个A 
        cout << "NO" << endl;
        // printf("NO\n");
        return;
    }
    int x = lp, y = lt - lp - 1, z = len - lt - 1;
    if (x*y == z)//a*b是否等于c
        cout << "YES" << endl;
    // printf("YES\n");
    else
        cout << "NO" << endl;
    // printf("NO\n");
}

int main()
{
    int count, i, j;
    char str[10][100];
    cin >> count;// scanf("%d",&count);读取条数
    if (count >= 0 && count<10)
    {
        for (i = 0; i<count; i++)
        {
            cin >> str[i];// scanf("%s",str[i]);因为数据量不大就一口气读完了
        }
        for (i = 0; i<count; i++)
        {
            PAT(str[i]);//一口气输出
        }
    }
    system("pause");
    return 0;
}

·如果感觉上面的思路不好理解,下面这种动态处理的方式可能思路会更清晰

#include <cstring>
#include <iostream>
using namespace std;
int PAT(char* str)
{
        int a = 0, b = 0, c = 0;
        int i;
        int len = strlen(str);
        for (i=0;; i++)
        {//遇到A就计数,遇到P跳出,遇到其他或结尾退出
                if(str[i] == 'A')       a++;
                else if(str[i] == 'P')  {i++;break;}
                else return 0;
        }
        for ( ;; i++)
        {//同理,A计数,遇到T跳出,遇到其他或结尾退出
                if(str[i] == 'A')       b++;
                else if(str[i] == 'T')  {i++;break;}
                else return 0;
        }
        for( ; i<len; i++)
        {//还是一样,A计数直到结尾,遇到其他就退出
                if(str[i]=='A')  c++;
                else return 0;
        }
        if(b >= 1 && a*b == c)//如果b至少一个且a乘b等于c就是正确的
                return 1;
        else return 0;
}
int main()
{
    int count, i;
    char str[100];
    cin >> count;// 读取条数
    if (count >= 0 && count<10)
    {
        for (i = 0; i<count; i++)
        {
            cin >> str;//读一条数据
            if(PAT(str))  cout << "YES" << endl;
            else  cout << "NO" << endl;
        }
    }
}

·PythonCode,用的一样是偏动态的方式

# coding: utf-8
def PAT(str):
a = 0
b = 0
c = 0
i = 0
length = len(str)
str += '\n'
#before meet 'P',count ‘A’
while 1:
    if str[i] == 'A':
        a += 1
        i += 1
    elif str[i] == 'P':
        i += 1
        break
    else:
        return False
#before meet 'T',count 'A'    
while 1:
    if(str[i] == 'A'):
        b += 1
        i += 1
    elif(str[i] == 'T'):
        i += 1
        break
    else:
        return False
#before end,count 'A'
while i < length:
    if(str[i]=='A'):
        c += 1
        i += 1
    else:
        return False
#only when count a*b==c and b >=1 return True
if(b>=1 and a*b == c):
    return True
else:
    return False
#input
count = input()
if (count >= 0 & count<10):
for i in range(count):
    str=raw_input()
    if(PAT(str)):
        print "YES"
    else:
        print "NO"

其实还有一种更动态的方式:
在接收到T之后,就已经确定了T后面A的期望个数,如果遇到结束符之后达到期望就直接输出‘YES’,就是不存储字符串,改用接收字符的完全动态处理,但是除了减少一些内存占用之外,过程其实都差不多,就不多写了,比较追求这个的同学可以试一下。

有疑问?查看帮助

相关文章

  • 1003. 我要通过!

    “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下...

  • 1003.我要通过!

    判断规则 这道题一共有三个规则: 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;任意形如 xPAT...

  • 1003. 我要通过!

    原题链接我要通过!: “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— ...

  • 1003. 我要通过!(20)

    “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下...

  • 1003. 我要通过!(20)

    传送门 PAT (Basic Level) Practise (中文)1003. 我要通过!(20) 题目 “答案...

  • PAT-B 1003. 我要通过!(20)

    传送门 https://pintia.cn/problem-sets/994805260223102976/pro...

  • 2018-04-08

    1003. 我要通过!(20) “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派...

  • 1003. Triangle Partition

    1003. Triangle Partition Time limit: 1 second Memory limi...

  • 20221015专业英语

    1001. bore diameter 孔径 1002. flange diameter法兰直径 1003. ni...

  • 我要通过!

    “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下...

网友评论

      本文标题:1003. 我要通过!

      本文链接:https://www.haomeiwen.com/subject/puivixtx.html