PAT Grade B B1003 - I'm going to pass! (20)


" 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

Ideas:
1. According to the second clause xPATx, where x is any number of A, we know:
    PAT...correct
    APATA...correct
    AAAPATAAA...correct
    ......
2. According to Article 3, if aPbTc is correct, then aPbATca is also correct, we know:
    PAT is correct, then...PAAT is correct...PAAAT is correct,
    APATA is correct, then...APAATAA is correct...APAAATAAA is correct,
    AAAPATAAA is correct, then AAAPAATAAAAAA is correct.

    It can be determined in this way that three variables left, mid and right are set to record the number of A on the left side of P, between PT and on the right side of T, respectively. Then in the case of decrement, let right minus the length of left, and the length of mid minus 1. Until
    Until right<=left, if condition 2 is satisfied at this time, the judgment is correct. Note that PAAT is correct when left and right are empty.

3. Set up a counter for each of P, A, T and other letters, and traverse the string to count the number. If there are other letters or the number of letter A is zero, output NO.

4. String processing uses string.substr() and string.find() to intercept and locate.
    Reference study: click to open the link

Reference Code:
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
intmain()
{
	int n;
	string s;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		cin>>s;
		int flagP=0,flagA=0,flagT=0,other=0;
		 for(int j=0;j<s.size();j++)
		 {
		 	if(s[j]=='P') flagP++;
		 	else if(s[j]=='A') flagA++;
			else if(s[j]=='T') flagT++;
			else other++;
		 }
		 if(other||flagP!=1||flagT!=1||flagA==0) {
		 printf("NO\n");
		 continue;
		 }
		 string left="",mid="",right="";
		 left=s.substr(0,s.find('P'));
		 mid=s.substr(s.find('P')+1,s.find('T')-s.find('P')-1);
		 right=s.substr(s.find('T')+1);
		 while(right.size()>left.size()&&mid.size()>=1){
		 	right=right.substr(0,right.size()-left.size());
		 	mid=mid.substr(0,mid.size()-1);
		 }
		 if(left==right&&mid.size()==1) printf("YES\n");
		 else if(left.size()==0&&right.size()==0&&mid.size()>0)printf("YES\n");
		 else printf("NO\n");
	}
	return 0;
}



Guess you like

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