PAT Basic 1003 I want to pass! (20 points)

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

Get the "correct answer" conditions are:

  1. String must have only  PATthree characters can not contain other characters;
  2. Arbitrary shape such as  xPATx a string can get "correct answer", wherein  x either the empty string, or only by the letter  A string thereof;
  3. If you  aPbTc are correct, then  aPbATca it is correct, which  abc are either empty string, or only by the letter  A string composed.

Now ask you to write a PAT referee program automatically determines which strings can get "the answer right".

Input formats:

Each test comprises a test input. The first row is given a positive integer  n-( <), the number is the character string 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 the output  NO.

Sample input:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

#include <iostream>
using namespace std;
int main(){
    int T;
    string tmp;
    cin>>T;
    while(T--){
        bool no=false;
        cin>>tmp;
        //检测包含其他字符
        for(int i=0;i<tmp.length();i++){
            if(tmp[i]!='P'&&tmp[i]!='A'&&tmp[i]!='T'){
                no=true;break;
            }
        }
        int P=tmp.find('P');
        int T=tmp.find('T');
        if(P==tmp.npos||T==tmp.npos){
            no=true;
        }
        int front=P;
        int middle=T-P-1;
        int after=tmp.length()-T-1;
        if(no||middle==0) cout<<"NO"<<endl;
        else if(front*middle==after) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11372053.html
Recommended