1003 我要通过!(JAVA) 20分

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.*;
import java.io.*;
/**
 * @author huangbentai
 * @date 2022/9/21 00:50
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
        String count = in.readLine();
        Integer n = Integer.parseInt(count);


        for(int i = 0; i < n; i++){

            String temp = in.readLine();
            //三段A的位置0:第一段A的长度;1:第二段A的长度,2:第三段A的长度
            int[] counts = new int[3];
            //PT的数量
            int[] PTcount = new int[2];
            //PT的位置
            int[] PTLocation = new int[2];
            //三段A的位置下标
            int x = 0;
            //是否符合条件判断
            boolean isNotRight = false;
            for (int j = 0; j < temp.length(); j++) {

                if (temp.charAt(j) == 'A'){
                    counts[x]++;
                }
                // 记录P位置,并且把三段A中从第一段移动到第二段A的位置
                if (temp.charAt(j) == 'P'){
                    PTLocation[0] = j;
                    PTcount[0]++;
                    if (PTcount[0] > 1){
                        isNotRight = true;
                        break;
                    }
                    x++;
                }
                // 记录T位置,并且把三段A中从第二段移动到第三段A的位置
                if (temp.charAt(j) == 'T'){
                    PTcount[1]++;
                    PTLocation[1] = j;
                    if (PTcount[1] > 1){
                        isNotRight = true;
                        break;
                    }
                    x++;
                }
                // 如果出现不等于P A T的字符直接判断不符合
                if (temp.charAt(j) != 'P' && temp.charAt(j) != 'A' && temp.charAt(j) != 'T'){
                    isNotRight = true;
                    break;
                }
            }
            // T在P前面不符合
            if (PTLocation[0] > PTLocation[1]){
                System.out.println("NO");
                continue;
            }
            if (isNotRight){
                System.out.println("NO");
                continue;
            }
            //P和T的数量不唯一不符合
            if (PTcount[0] != 1 || PTcount[1] != 1){
                System.out.println("NO");
                continue;
            }
            // PT中间必须有A
            if (counts[1] == 0){
                System.out.println("NO");
                continue;
            }
            // 第一段A 的数量 * 第二段 A的数量 等于 第三段 A的数量符合条件
            if ((counts[0] * counts[1]) == counts[2]){
                System.out.println("YES");
                continue;
            }else {
                System.out.println("NO");
            }


        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值