By 1003 I want to! (20 points)

" 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:

  1. String must have only P, A, Tthree characters can not contain other characters;
  2. Arbitrary shape such as xPATxa string can get " correct answer ", wherein xeither the empty string, or only by the letter Astring thereof;
  3. If you aPbTcare correct, then aPbATcait is correct, which a, b, care either empty string, or only by the letter Astring composed.

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 the output NO.

Sample input:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

      
    

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

Code

// b1003-我要通过!.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// PAT  APATA AAPATAA  AAAPATAAA AAAAPAATAAAAAAAA
// 由题意可知,p前面的A的个数①乘以中间的A的个数②等于后面的A的个数③

#include <iostream>
using namespace std;

bool judge(string str) {
    int a1 = 0, a2 = 0, a3 = 0;//统计三个位置a的个数
    int p = 0, t = 0;

    for (int i = 0; i < str.length(); i++) {
        if (p == 0) {
            //第一段
            if (str[i] == 'A') {
                a1++;
            }
            else if (str[i] == 'P') {
                p = 1;
            }
            else {
                return false;
            }
        }
        else {
            if (t == 0) {
                //中间那段
                if (str[i] == 'A') {
                    a2++;
                }
                else if (str[i] == 'T') {
                    t = 1;
                }
                else {
                    return false;
                }
            }
            else {
                //最后一段
                if (str[i] == 'A') {
                    a3++;
                }
                else {
                    return false;
                }
            }
        }
    }
    if (a2 < 1) {
        return false;
    }
    if (a1 * a2 == a3) {
        return true;
    }
    else {
        return false;
    }
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string str;
        cin >> str;
        if (judge(str)) {
            cout << "YES"<<endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ericling/p/12325482.html
Recommended