PAT (Basic Level) Practise (中文) 1003.我要通过
2016-10-18 18:16
357 查看
1003. 我要通过!(20)
时间限制400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于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
解题思路
根据条件2,形如PAT、APATA、AAPATAA的字符串可以通过;
条件3是建立在条件2的基础之上的,动笔递推:
PAT -> PAAT -> PAAAT -> ... 中间A的数目每次加1
APATA -> APAATAA -> APAAATAAA -> ... 中间A的数目每次加1,右边A的数目每次加1(左边A的数目)
AAPATAA -> AAPAATAAAA -> AAPAAATAAAAAA -> ... 中间A的数目每次加1,右边A的数目每次加2(左边A的数目)
··· ···
结论:左边A的数目 * 中间A的数目 = 右边A的数目。
AC代码如下:
#include <iostream> using namespace std; int main() { int n; cin>>n; while( n-- ){ int num_left=0,num_center=0,num_right=0; string str; cin>>str; int len=str.length(),num_P=0,num_T=0,num_A=0; //检查字符串结构 : 1个'P' + 1个'T' + 至少1个'A' int flag=1; for( int i=0;i<len;++i ){ if( str[i]!='P' && str[i]!='A' && str[i]!='T' ){ flag=0; break; } if( str[i]=='A' ){ ++num_A; } else if( str[i]=='P' ){ ++num_P; } else if( str[i]=='T' ){ ++num_T; } } if( flag==0 || num_P!=1 || num_T!=1 || num_A<1 ){ cout<<"NO"<<endl; continue; } if( num_A==1 && str!="PAT" ){//"APT"是个坑! cout<<"NO"<<endl; continue; } int i; //求P左侧'A'的数目 for( i=0;i<len;++i ){ if( str[i]=='A' ){ ++num_left; } else if( str[i]=='P' ){ break; } } //求P,T中间'A'的数目 for( i+=1;i<len;++i ){ if( str[i]=='A' ){ ++num_center; } else if( str[i]=='T' ){ break; } } //求T右侧'A'的数目 for( i+=1;i<len;++i ){ if( str[i]=='A' ){ ++num_right; } else if( str[i]=='T' ){ break; } } if( num_left*num_center==num_right ){ cout<<"YES"<<endl; } else{ cout<<"NO"<<endl; } } return 0; }
相关文章推荐
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- 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乙级真题及训练集 PAT (Basic Level) Practise (中文)1003. 我要通过!
- PAT (Basic Level) Practise (中文)- 1003. 我要通过!(20)
- PAT (Basic Level) Practise 1003. 我要通过!
- PAT (Basic Level) Practise (中文)1031. 查验身份证(15)
- PAT (Basic Level) Practise (中文)1002. 写出这个数 (20)
- PAT (Basic Level) Practise (中文)1037. 在霍格沃茨找零钱(20)
- PAT (Basic Level) Practise (中文)1058. 选择题(20)
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)1040. 有几个PAT(25)
- PAT (Basic Level) Practise (中文)- 1018. 锤子剪刀布 (20)
- PAT (Basic Level) Practise (中文)- 1004. 成绩排名 (20)
- PAT (Basic Level) Practise (中文) 1009.说反话
- PAT (Basic Level) Practise (中文) 1014. 福尔摩斯的约会 (20)
- PAT (Basic Level) Practise (中文)1006. 换个格式输出整数 (15) C语言