PAT [B] I want to pass!

Subject description:

" Right answer " is the most joy automatic reply sentence topic given system. This title belongs to the PAT of " right answer " big delivery - just read the string following conditions are satisfied, the system will output " right answer ", otherwise outputs " Wrong Answer ."

Get the " right answer " conditions are:

String must have only P, A, T three character, other characters can not contain;
arbitrary shape such as a string can be obtained xPATx "correct answer", or where x is the empty string, or only by the letter a string thereof;
if aPbTc is correct, then aPbATca is correct, wherein a, b, c or all the empty string, or a string consisting only of letters a.
Now ask you to write a PAT referee program automatically determines which strings can get " correct answer " is.

Input formats:

Each test comprises a test input. Line 1 is given a positive integer n (<10), is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.

Output formats:

The detection result row for each string, if the string can get "correct answer", the output YES, otherwise output NO.

Sample input:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

Problem-solving ideas:

condition:

  1. String must have only P, A, T of the three characters, the characters may not contain other;
  2. The string of arbitrary shape can be obtained xPATx "correct answer", or where x is the empty string, or a string consisting only of letters A;
  3. If aPbTc is correct, then aPbATca is correct, wherein a, b, c or all the empty string, or a string consisting only of letters A.

The first one condition: You can not have other characters, the better to judge

The second condition: xPATx: Description P, T and only one, P in front of the A and T A is equal to the latter.

Third condition: if aPbTc correct, adding a b A is A, P and T intermediate, then aPbATcawe continue to push down a, P and T and then add an intermediate A, then aPbAATcaa, the number of A T is equal to the back a number of P multiplied by the number of front between the P and a T, the second condition is satisfied.

Code:

#include<iostream>
#include<string>
using namespace std;
int main() {
    string s;
    int n;
    int i,a,b,c;
    cin>>n;
    while (n--) {
        cin>>s;
        i=a=b=c=0;
        if(s=="PAT") {
            cout<<"YES"<<endl;
            continue;
        }
        while(s[i]=='A') {
        	i++;
        	a++;
		}
		if(s[i]=='P') {
			i++;
			while(s[i]=='A') {
				i++;
				b++;
			}
			if(s[i]=='T') {
				i++;
				while(s[i]=='A') {
					i++;
					c++;
				}
				if(c==a*b&&b>0) {
					cout<<"YES"<<endl;
		            continue;
				}
			}	
		}
		cout<<"NO"<<endl;
    }
    return 0;
}
Published 55 original articles · won praise 30 · views 9811

Guess you like

Origin blog.csdn.net/chaifang0620/article/details/104873203