1003 我要通过! (20 分)
package testSystem;
import java.util.Scanner;
//注意的要点
/*
* 1.P和T只能出现一次,且P的位置比T前,且不能出现 PT 这种情况
* 2.找规律:p之前的A数量*P和T中间A的数量=后面A的数量
* 3.只能有P A T这三种字母组成组成
* */
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt(); //测试字符串的个数
String[] s = new String[N];
//读入
for (int i = 0; i < N; i++) {
s[i] = in.next(); //nextLine()表示输入后如果换行,表示输入已经结束。
}
//输出
int countP, countA, countT, positionP, positionT;
char[] c;
for (int j = 0; j < N; j++) {
c = s[j].toCharArray();
countA = 0;
countP = 0;
countT = 0;
positionP = 0;
positionT = 0;
for (int k = 0; k < c.length; k++) {
if (c[k] == 'P') {
countP++;
positionP = j;//记录p的位置
}
if (c[k] == 'A') {
countA++;
}
if (c[k] == 'T') {
countT++;
positionT = j;//记录T的位置
}
}
//判断输出
if ((countA + countP + countT == c.length) && (positionT - positionP > 1) && (countP == 1) && (countT == 1) && (positionP * (positionT - positionP - 1) == c.length - positionT - 1)) {//重点是这句
System.out.println("Yes");
} else {
System.out.println("NO");
}
}
}
}
备注:代码有Bug,但是思想是对的。