# PAT-BASIC1003——我要通过

### 思路：条件3是一个递归的定义

（1）如果其既满足条件1又满足条件2，则可以输出正确。

（2）如果“aPbTc”是正确的，那么也可以输出正确。那么我们如何去判断“aPbTc”正确呢？这又回到了一开始的如何判断一个字符串是“答案正确”还是“答案错误”这个原问题。递归逻辑就此形成。

C++代码：

#include<iostream>
#include<string>

using namespace std;

bool conditionFirst(string s);
bool conditionSecond(string s);
bool conditionThird(string s);
bool condition(string s);

int main() {
int count;
cin >> count;
string input;

for (int i = 0; i < count; i++) {
cin >> input;
bool result = condition(input);
if (result) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}

bool onlyContainA(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != 'A') {
return false;
}
}
return true;
}

bool conditionFirst(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != 'P' && s[i] != 'A' && s[i] != 'T') {
return false;
}
}
return true;
}

bool conditionSecond(string s) {
int indexP = -1;
for (int i = 0; i < s.length() - 2; i++) {
if (s[i] == 'P' && s[i + 1] == 'A' && s[i + 2] == 'T') {
indexP = i;
break;
}
}
if (indexP == -1) {
return false;
}
string x1 = s.substr(0, indexP);
if (!onlyContainA(x1)) {
return false;
}
string x2 = s.substr(indexP + 3, s.length() - (indexP + 3));
if (x1.compare(x2) == 0) {
return true;
} else {
return false;
}
}

bool conditionThird(string s) {
int indexP = -1;
for (int i = 0; i < s.length() - 2; i++) {
if (s[i] == 'P') {
indexP = i;
break;
}
}
if (indexP == -1) {
return false;
}
int indexT = -1;
for (int i = 1; i < s.length(); i++) {
if (s[i] == 'T') {
indexT = i;
break;
}
}
if (indexT == -1) {
return false;
}
if (s[indexT - 1] != 'A') {
return false;
}
string a = s.substr(0, indexP);
string b = s.substr(indexP + 1, indexT - 2 - indexP);
string ca = s.substr(indexT + 1, s.length() - 1 - indexT);
if (!onlyContainA(a) || !onlyContainA(b) || !onlyContainA(ca)) {
return false;
}
if (ca.length() < a.length()) {
return false;
}
string c = ca.substr(0, ca.length() - a.length());
string newS = a + "P" + b + "T" + c;
return condition(newS);
}

bool condition(string s) {
if (conditionFirst(s)) {
if (conditionSecond(s)) {
return true;
} else {
if (conditionThird(s)) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}



C++解题报告：

JAVA代码：

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int total = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < total; i++) {
StringBuilder stringBuilder = new StringBuilder(scanner.nextLine());
if(conditionFirst(stringBuilder)){
if(conditionSecond(stringBuilder)){
System.out.println("YES");
}else{
if(conditionThird(stringBuilder)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}else{
System.out.println("NO");
}
}
}

private static boolean conditionFirst(StringBuilder stringBuilder){
for (int i = 0; i < stringBuilder.length(); i++) {
char letter = stringBuilder.charAt(i);
if(letter != 'P' && letter != 'A' && letter != 'T'){
return false;
}
}
return true;
}

private static boolean conditionSecond(StringBuilder stringBuilder){
int indexP = stringBuilder.indexOf("PAT");
if(indexP == -1){
return false;
}
for (int i = 0; i < indexP; i++) {
if(stringBuilder.charAt(i) != 'A'){
return false;
}
}
for (int i = indexP + 3; i < stringBuilder.length(); i++) {
if(stringBuilder.charAt(i) != 'A'){
return false;
}
}
if(stringBuilder.substring(0, indexP).equals(stringBuilder.substring(indexP + 3))){
return true;
}else {
return false;
}
}

private static boolean conditionThird(StringBuilder stringBuilder){
int indexAT = stringBuilder.indexOf("AT");
if(indexAT == -1){
return false;
}
int indexP = stringBuilder.indexOf("P");
if(indexP == -1){
return false;
}
String a = stringBuilder.substring(0, indexP);
String b = stringBuilder.substring(indexP + 1, indexAT);
String ca = stringBuilder.substring(indexAT + 2, stringBuilder.length());
if(ca.length() < a.length()){
return false;
}
String c = ca.substring(0, ca.length() - a.length());
StringBuilder temp = new StringBuilder(a + "P" + b + "T" + c);
if(conditionFirst(temp)){
if(conditionSecond(temp)){
return true;
}else{
if(conditionThird(temp)){
return true;
}else{
return false;
}
}
}else{
return false;
}
}
}


JAVA解题报告：