“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
现在就请你为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
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int count_P,count_A,count_T;
int pos_P,pos_T;
String[] str = new String[n];
for ( int i = 0 ; i < n ; i++ ){
str[i] = in.next();
count_P = 0;
count_A = 0;
count_T = 0;
pos_P = 0;
pos_T = 0;
int len = str[i].length();
for ( int j = 0 ; j < len ; j++ ){
if ( str[i].substring(j, j+1).equals("P")){
count_P++;
pos_P = j;
}
if ( str[i].substring(j, j+1).equals("A"))
count_A++;
if ( str[i].substring(j, j+1).equals("T")){
count_T++;
pos_T = j;
}
}
if ( count_P+count_A+count_T != len|| pos_T-pos_P<=1 || count_P>1 || count_T>1 || pos_P*(pos_T-pos_P-1)!=len-pos_T-1){
System.out.println("NO");
}
else{
System.out.println("YES");
}
}
in.close();
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有