1003 我要通过! (20 分)
【思路】:只要P前面的A的个数乘以PT之间的A的个数的积等于T后面的A的个数且字符串中只含有单个P和T就为YES,否则NO。即条件三中符合a*b=c。
在写主类时一定要在前面加上“public”,否则牛客网的编译器会报错“找不到Main类”!
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- != 0){
String str = sc.next();
char[] chr = str.toCharArray();
int cnt_p=0, cnt_t=0, other=0;
int flag_p =0, flag_T=0;
int len = chr.length;
for(int i=0; i<len; i++){
if(chr[i] == 'P'){
cnt_p++;
flag_p = i;
}
else if(chr[i] == 'T'){
cnt_t++;
flag_T=i;
}
else if(chr[i] != 'A'){
other++;
}
}
if((cnt_p != 1) || (cnt_t != 1) || (other!=0) || (flag_T-flag_p <=1)){
System.out.println("NO");
}else if((flag_p * (flag_T-flag_p-1)) == (len - flag_T -1)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
PAT乙级 文章被收录于专栏
做题记录



查看10道真题和解析