PAT (Basic Level) 1003 我要通过!
问题描述:
答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
- 字符串中必须仅有
P
、A
、T
这三种字符,不可以包含其它字符; - 任意形如
xPATx
的字符串都可以获得“答案正确”,其中x
或者是空字符串,或者是仅由字母A
组成的字符串; - 如果
aPbTc
是正确的,那么aPbATca
也是正确的,其中a
、b
、c
均或者是空字符串,或者是仅由字母A
组成的字符串。
现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
输入格式:
每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。
输出格式:
每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出
YES,否则输出
NO。
输入样例:
[code]8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA
输出样例:
[code]YES YES YES YES NO NO NO NO
首先分析一下这个题吧,条件1和条件2就不必说了,关键在于条件3 (如果
aPbTc是正确的,那么
aPbATca也是正确的,其中
a、
b、
c均或者是空字符串,或者是仅由字母
A组成的字符串)。这句话的意思应该就是b中补充一个A则c中补充一个a串,由于a、b、c中均只能包含A字符,则a.length()、b.length()、c.length()满足a.length()*b.length()=c.length()。看了网上的大佬写的代码后有点懵(刚学C++还不太会),故先附上大佬代码
[code]#include<iostream> #include<string> #include<vector> using namespace std; int main(){ int num; vector<string> v; string s; cin>>num; for(int i = 0;i<num;i++){ cin>>s; size_t p = s.find_first_not_of("A"); if(p == string::npos ||s[p]之后就是自己写的扫描串的做法了,网上看了别人的代码,发现输入和输出格式有问题,不符合PAT要求,刚开始自己想通过字符串截取获得a、b、c串的长度,但是在C++还不会使(java实现可能简单)。!='P'){ v.push_back("NO"); continue; } size_t t= s.find_first_not_of("A",p+1); if(t== string::npos ||t==p+1 || s[t]!='T'){ v.push_back("NO"); continue; } size_t n = s.find_first_not_of("A",t+1); if(n != string::npos){ v.push_back("NO"); continue; } if((s.length()-1-t)==p*(t-p-1) ){ v.push_back("YES"); }else{ v.push_back("NO"); } } for(int i=0;i<num;i++){ cout<<v[i]<<endl; } return 0; }
附上原文地址:https://www.geek-share.com/detail/2674834638.html 但是他这个输入输出有问题并且是用C写的。
[code]#include <iostream> #include <string> using namespace std; int main() { int i,j,n; int count_P,count_A,count_T,pos_P,pos_T; cin>>n; string a ,b ; for(i=0;i<n;i++){ cin>>a[i]; count_P = 0; count_A = 0; count_T = 0; pos_P = 0; pos_T = 0; for(j=0;j<a[i].length();j++){ if(a[i][j]=='P'){ count_P++; pos_P = j; //记录p的位置 } if(a[i][j]=='A') count_A++; if(a[i][j]=='T'){ count_T++; pos_T = j; //记录T的位置 为了计算a、b、c串的长度 } } if(count_P+count_A+count_T != a[i].length() || pos_T-pos_P<=1 || count_P>1 || count_T>1 || pos_P*(pos_T-pos_P-1)!=a[i].length()-pos_T-1) b[i]="NO"; else b[i]="YES"; } for(i=0;i<n;i++){ cout<<b[i]<<endl; } return 0; }
- PAT BASIC LEVEL 1003. 我要通过!(20)
- PAT(basic level) 1003 我要通过
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- PAT(Basic Level)1003 我要通过!
- PAT (Basic Level) Practise 1003. 我要通过!
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1003. 我要通过!
- PAT (Basic Level)1003.我要通过
- PAT 1003 我要通过! (Basic Level)
- PAT Basic Level 1003 我要通过! (20 分)
- PAT (Basic Level) Practise (中文)- 1003. 我要通过!(20)
- PAT Basic Level 1003. 我要通过!(20)
- PAT(Basic Level)_1003_我要通过!
- PAT (Basic Level) Practise (中文) 1003.我要通过
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20) C语言
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- PAT (Basic Level) Practise (中文) 1003.我要通过!(20)
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- Pat(Basic Level)Practice--1003(我要通过)
- PAT (Basic Level) Practice (中文)1003 我要通过!