PAT乙级1003 测试点2 不通过 JAVA
原题链接:https://pintia.cn/problem-sets/994805260223102976/exam/problems/994805323154440192
我的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class pat1003 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int firstP, lastT, len;
String str;
String first_str, medium_str, last_str;
while (n-- > 0) {
str = br.readLine();
len = str.length();
if (str.equals("PAT")) {
System.out.println("YES");
continue;
}
if (str.contains("P") && str.contains("T")) {
firstP = str.indexOf('P');
lastT = str.indexOf('T');
first_str = str.substring(0, firstP);
medium_str = str.substring(firstP + 1, lastT);
last_str = str.substring(lastT + 1, len);
if (OnlyA(first_str) && OnlyA(medium_str) && OnlyA(last_str) && medium_str.contains("A")
&& first_str.length() * medium_str.length() == last_str.length()) {
System.out.println("YES");
continue;
}
}
System.out.println("NO");
}
br.close();
}
public static boolean OnlyA(String s) {
if (s.equals(""))
return true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != 'A')
return false;
}
return true;
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int num =scan.nextInt();
String str[] =new String[num];
for(int i =0;i<num;i++) {
str[i]=scan.next();
}
for(int i=0;i<num;i++) {
TrueFalse(str[i]);
}
}
public static void TrueFalse(String s) {
int numa=0;
int nump=0;
int numt=0;
int destp=0;
int destt=0;
for(int j = 0;j<s.length();j++){
if(s.charAt(j)=='A'){
numa+=1;
}else if(s.charAt(j)=='P'){
nump+=1;
destp=j;
}else if(s.charAt(j)=='T'){
numt+=1;
destt=j;
}
}
if(numa+nump+numt==s.length()&&nump==1&&numt==1&&destt-destp>1 && numa!=0&& destp*(destt-destp-1)==s.length()-destt-1) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}