One day two PAT (1)

In fact, since yesterday brush, preparing for what PAT (B, chicken dishes do not explain, I hope a few months can grow in it), do a bit and found his poor arithmetic skills good ah. . . . .

First on the subject

1. The string must only P, A, T three character, other characters can not contain;
2 xPATx arbitrary shape such as a string can get "correct answer", or where x is an empty string, or a string is composed only of letters;
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.
Now ask you to write a PAT referee program automatically determines which strings can get "the answer right".

Input format: Each test comprises a test input. Line 1 shows a natural number 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


Again on my code
#include<iostream>
#include<string>
using namespace std;

int main ()
{
int n;
cin>>n;
string *a;
int j,n1=0,n2=0,n3=0;
a = new string[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{ j=0;
n1=0;
n2=0;
n3=0;
if(a[i][j]=='P')
{ j++;
if(a[i][j]!='A')
{
cout<<"NO"<<endl;
continue;
}
while(a[i][j]=='A'&&j<a[i].length())
{
j++;
}
if(j==a[i].length())
{
cout<<"NO"<<endl;
continue;
}
else if(a[i][j]=='T'&&(j+1)==a[i].length())
{
cout<<"YES"<<endl;
continue;
}
else
{
cout<<"NO"<<endl;
continue;
}
}
while(j<a[i].length()&&a[i][j]=='A')
{
//cout<<j<<n1<<endl;
j++;
n1++;
}
if(j==a[i].length())
{
cout<<"NO"<<endl;
continue;
}
if(a[i][j]!='P')
{
cout<<"NO"<<endl;
continue;
}
j++;
while(j<a[i].length()&&a[i][j]=='A')
{
j++;
n2 ++;
}
if(j==a[i].length())
{
cout<<"NO"<<endl;
continue;
}
if(a[i][j]!='T')
{
cout<<"NO"<<endl;
continue;
}
n3 = n1 * n2;
j++;
if((n1+n2+n3+2)!= a[i].length())
{
cout<<"NO"<<endl;
continue;
}
while(j<a[i].length())
{
if(a[i][j]!='A')
{
cout<<"NO"<<endl;
break;
}
j++;
}
if(j==a[i].length())
{
cout<<"YES"<<endl;
}
}
delete []a;
}

Good food ah, checked the others, get 28 rows. . . . . .

Guess you like

Origin www.cnblogs.com/guairenkuangren/p/12070223.html