PAT Class B Java Implementation_1003. I want to pass_With detailed problem solving notes_03

1003. I want to pass! (20)

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

" Correct answer " is the most pleasing response from the automated question answering system. This question belongs to PAT's " correct answer " big 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. There must be only three characters of P, A, T in the string, and cannot contain other characters;
2. Any string in the form of xPATx can get " correct answer ", where x is either an empty string, or is a string consisting only of the letter A;
3. If aPbTc is true, then aPbATca is also true, where a, b, c are either an empty string, or a string consisting only of 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. Line 1 gives a natural number n (<10), which is the number of strings to be detected. Next, each string occupies one line, and 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 get " correct answer ", output YES, otherwise output NO.

Input sample:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
Sample output:
YES
YES
YES
YES
NO
NO
NO
NO


-------------------------------------------------- --Dividing line---------------------------------------------- --------------

Idea: Condition 1 must be satisfied, and it is easy to write. The emphasis is on the understanding of the second and third conditions.

Each string that meets condition 2 can continue to push out new strings according to condition 3, and the pushed strings can continue to push.

So you can try to list a few and find a pattern.

PAT This string meets condition 2, then the string derived from condition 3 is PAAT, and then continues to push PAAAT, that is:

PAT ---> PAAT ---> PAAAT ---> PAAAAT

APATA ---> APAATAA ---> APAAATAAA ---> APAAAATAAAA

AAPATAA ---> AAPAATAAAA ----> AAPAAATAAAAAA ----> AAPAAAATAAAAAAA 

It can be seen that the values ​​of a, b, and c in the qualified string aPbTc will always satisfy the relationship a * b = c.

import java.util.Scanner;
public class PAT_B_1003//The class name needs to be changed to Main when submitting on the PAT platform
{
	public static void main(String[] agrs)
	{
		Scanner in = new Scanner(System.in);//Read all input
		int n = in.nextInt();//读取输入的第一个数字,代表了后面输入的样例数
		in.nextLine();//清除数字后的回车键
		
		for(int i = 0; i < n; i++)//对每个样例都判断
		{
			String str = in.nextLine();//输入的样例字符串
			if(onlyPAT(str))//如果只含有PAT三种字符,则判断是否符合第2、3个条件
			{
				if(aPbTc(str))
				{
					yes();//符合输出YES
				}
				else
				{
					no();//不符合输出NO
				}
			}
			else//不止含有PAT三种字符,输出NO
			{
				no();
			}
		}
	}
	
	public static void yes()//输出YES
	{
		System.out.println("YES");
	}
	
	public static void no()//输出NO
	{
		System.out.println("NO");
	}
	
	public static boolean onlyPAT(String str)
	{//将字符串遍历一遍,有不是PAT字符的则返回false
		for(int i = 0; i < str.length(); i++)
		{
			if(str.charAt(i) != 'P' && str.charAt(i) != 'A' && str.charAt(i) != 'T')
			{
				return false;
			}
		}
		return true;
	}
	
	public static boolean aPbTc(String str)
	{
		int a = str.indexOf("P");//aPbTc string, the value of a represents the number of A before the character P
		int c = str.length() - str.indexOf("T") - 1;//aPbTc In the string, the value of c represents the number of A after the character T
		int b = str.indexOf("T") - str.indexOf("P") - 1;//aPbTc string, the value of b represents the number of A between P and T
		if((b == 0 ) || (a * b != c))//It can be seen from the example that b cannot be 0
			return false;
		else
			return true;
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691944&siteId=291194637