1003. 我要通过!(20)
2018-01-22 14:34
155 查看
PAT (Basic Level) Practise (中文)
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
c++:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string num[10];
int testNum;
cin>>testNum;
string resultCh[2] = {"YES","NO"};
for(int i = 0;i<testNum;++i)
{
cin>>num[i];
}
for(int i = 0;i<testNum;++i)
{
int result = 1;
int locationP = -100,locationT=-100;
int countP=0,countT=0,countA=0;
for(int j = 0;j<num[i].size();++j)
{
if(num[i][j]!='P'&&num[i][j]!='T'&&num[i][j]!='A'&&num[i][j]!='\n')
break;
if(num[i][j]=='P'){
locationP = j;
countP ++;
}
if(num[i][j]=='T'){
locationT = j;
countT ++;
}
if(num[i][j]=='A')
countA ++;
if(countP>1||countT>1)
break;
}
//if((locationP+locationT+1)==num[i].size()||((2*locationP+locationT+1)==num[i].size()&&(locationT-locationP)==3))
//这里的规律一直没想透,看了别人的代码才明白P之前的字符数*PT之间的字符=T后的字符数
if((locationP*(locationT-locationP-1))==(num[i].size()-locationT-1)&&countA>0)
result = 0;
cout<<resultCh[result]<<endl;
}
return 0;
}
Java:
import java.util.Scanner; public class Main { public static void main(String[] args){ String[] testString = new String [10]; Scanner input = new Scanner(System.in); int testNum = input.nextInt(); String[] resultCh = {"YES","NO"}; for(int i = 0;i<testNum;++i){ testString[i] = input.next(); } for(String eachString : testString){ int result = 1; int posP = -100,posT = -100; int countP = 0,countA = 0,countT =0 ; for(int j = 0;j<eachString.length();++j){ if(eac aab6 hString.charAt(j)!='P'&&eachString.charAt(j)!='A'&&eachString.charAt(j)!='T'&&eachString.charAt(j)!='\n') break; if(eachString.charAt(j)=='P'){ countP++; posP = j; } if(eachString.charAt(j)=='T'){ countT++; posT = j; } if(eachString.charAt(j)=='A') countA++; if(countP>1||countT>1) break; }//end inner loop if((posP*(posT-posP-1))==(eachString.length()-posT-1)&&countA>0) result = 0; System.out.println(resultCh[result]); }//end outer loop } }
相关文章推荐
- PAT 1003. 我要通过!(20) java实现
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- PAT (Basic Level) Practise (中文)- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- PAT-B 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20) C语言
- [PAT]1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20) (ZJUPAT 模拟)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- 1003. 我要通过!(20)
- PAT乙级 1003. 我要通过!(20)