PTA class B 1003JAVA problem solving

Posted by newmember on Mon, 10 Jan 2022 10:58:22 +0100

1003

1003 I want to pass! (20 points)

"Correct answer" is the most gratifying reply given by the automatic question judgment system. This question belongs to PAT's "correct answer" distribution - as long as the read string meets the following conditions, the system will output "correct answer", otherwise it will output "wrong answer".

The conditions for getting "correct answer" are:

  1. The string must contain only three characters: P, A and T, and cannot contain other characters;

  2. Any string shaped like xPATx can get "correct answer", where x is either an empty string or A string composed of only the letter A;

  3. If aPbTc is correct, aPbATca is also correct, where a, b and c are either empty strings or strings composed of only the letter A.

Now please write an automatic referee program for PAT to determine which strings can get the "correct answer".

Input format:

Each test input contains 1 test case. The first line gives a positive integer n (≤ 10), which is the number of strings to be detected. Next, each string occupies one line. The length of the string does not exceed 100 and does not contain spaces.

Output format:

The detection result of each string occupies one line. If the string can obtain "correct answer", output YES, otherwise output NO.

Input example:

10
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
APT
APATTAA
No blank lines at the end

Output example:

YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
No blank lines at the end

analysis

The first condition is that there must be three letters P A T, the second condition is that there is an a in the middle of the letter P T, and then it is correct that there are empty strings on both sides, or a string with the same length on both sides. The third condition is that a, b and c are both a strings or empty strings. Assuming that they are all a strings, under the condition that aPbTc is correct, an a character is added between P and t, Then, it is correct only in the case of aPbATca. From here, it can be found that if an a character is added between P and T, a string of a length is added. Therefore, it is found that the length of c string is equal to the length of a string multiplied by the length of b string. The most important thing in this problem is to find the law of the third condition, and then program in combination with the three conditions. All input strings can be transformed into aPbTc string types for judgment.

code

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n,p,t;
        int value=0;
        n=sc.nextInt();
        Map<Character, Integer> m=new HashMap<Character, Integer>();//use map Set to statistics P T A Number of three letters
        m.put('A',0);
        m.put('P',0);
        m.put('T',0);
        m.put('X',0);
        String s;//Defines the string to be entered each time
        int lenA=0;//aPbTc This type corresponds to three types A The length of the string, lenA corresponding a
        int lenB=0;//corresponding b
        int lenC=0;//corresponding c
        int[] a=new int[n+1];//Set an array to judge whether each input string meets three conditions. If so, let the corresponding value be 1, otherwise it is 0
        for(int i=1;i<=n;i++){
            s=sc.next();
        for(int j=0;j<s.length();j++){
            if(s.charAt(j)=='A'){//Statistics A Number of strings
                value=m.get('A');
                value++;//Give Way A Length of string plus 1
                m.put('A',value);
            }
            else if(s.charAt(j)=='P'){//When traversing each character in turn, an error was encountered P Characters, that is, you can take them a The length of the string, lenA
               value=m.get('P');//Statistics P Number of occurrences
               value++;
                m.put('P',value);
                lenA=m.get('A');
                m.put('A',0);//Got it a After the length of the string, let map In the collection A The number of is 0, which will be counted later b Length of string
            }
            else if(s.charAt(j)=='T'){//When traversing each character in turn, an error was encountered P Characters, that is, you can take them b The length of the string, lenB
                value=m.get('T');//Statistics T Number of occurrences
                value++;
                m.put('T',value);
                m.put('T',1);
                lenB=m.get('A');
                m.put('A',0);//Got it b After the length of the string, let map In the collection A The number of is 0, which will be counted later c Length of string
            }else{
                value=m.get('X');//use x Character to replace the statistics of other characters
                value++;
                m.put('X',value);
            }

        }
        lenC=m.get('A');//After one traversal, map In collection A The value corresponding to the key is c Length of string
        if(lenC==lenA*lenB&&m.get('P')==1&&m.get('T')==1&&m.get('X')==0&&lenB!=0){
         //Judgment condition III        There is only one P T That's right    Judge whether there are other characters. If there is no heuristic character, you can correctly judge whether there is the whole string A Character, judgment condition 1
            a[i]=1;
        }
            m.put('A',0);//Let the set data be zero and recalculate the next string
            m.put('P',0);
            m.put('T',0);
            m.put('X',0);
            lenA=0;//Will statistics a b c The length of the string is set to zero to count the next string
            lenB=0;
            lenC=0;
        }
       for(int i=1;i<=n;i++){//Output results in sequence
           if(a[i]==1){
               if(i==n){
                   System.out.print("YES");
               }else {
                   System.out.println("YES");
               }

           }else{
               if(i==n){
                   System.out.print("NO");
               }else {
                   System.out.println("NO");
               }
           }
       }
    }
}