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乙级 文章被收录于专栏

做题记录

全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务