• PAT 1003. 我要通过!(20) JAVA


    参考http://blog.csdn.net/bin8632/article/details/50216297

    答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

    得到“答案正确”的条件是:

    1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
    2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
    3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

    现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

    输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。

    输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。

    输入样例:

    8
    PAT
    PAAT
    AAPATAA
    AAPAATAAAA
    xPATx
    PT
    Whatever
    APAAATAA
    

    输出样例:

    YES
    YES
    YES
    YES
    NO
    NO
    NO
    NO
     1 import java.util.ArrayList;
     2 import java.util.Scanner;
     3 /**
     4  * T之后A的数量减去P之前A的数量/P之前A的数量赢等于P与T之间A的数量
     5  * @author China
     6  *
     7  */
     8 public class IWantToPass {
     9     public static void main(String[] args) {
    10         int number;
    11         String string = new String();
    12         Scanner sc = new Scanner(System.in);
    13         number = Integer.parseInt(sc.nextLine());
    14         for(int i=0;i<number;i++)
    15         {
    16             string = sc.nextLine();
    17             String pattern = "A*PA+TA*";//匹配3个条件
    18             String pattern1 = "PA+T";//a,c均为空字符串
    19             if(string.matches(pattern)){
    20                 if(string.matches(pattern1)){
    21                     System.out.println("YES");
    22                     continue;
    23                 }
    24                 else{
    25                     String temp[] = string.split("P|T");
    26                     int a = temp[0].length();//P之前A的数量
    27                     int b = temp[1].length();//P之后T之前A的数量
    28                     int c = temp[2].length();//T之后A的数量
    29                     if((c-a)/a==b-1){
    30                         System.out.println("YES");
    31                     }
    32                     else{
    33                         System.out.println("NO");
    34                     }                
    35                     }
    36             }
    37             else
    38                 System.out.println("NO");
    39         }
    40         
    41     }
    42 }


  • 相关阅读:
    洛谷 P2695 骑士的工作
    洛谷 P2839 畅通工程
    hdu_5742_It's All In The Mind
    hdu_5734_Acperience
    hdu_5738_Eureka(脑洞)
    hdu_5724_Chess(组合博弈)
    Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
    Codeforces Round #363 (Div. 2) B. One Bomb (水题)
    Codeforces Round #363 (Div. 2) C. Vacations(DP)
    hdu_5723_Abandoned country(最小生成树)
  • 原文地址:https://www.cnblogs.com/lolybj/p/6146347.html
Copyright © 2020-2023  润新知