我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192
题目描述:
知识点:递归
思路:条件3是一个递归的定义
如何判断一个字符串是“答案正确”还是“答案错误”呢?
如果要正确必须满足条件1和条件2。如果不满足条件1和条件2,但是满足条件3,也是正确的。那么条件3说的是什么呢?
对于“aPbATca”型的字符串,我们判断其是否正确时,要从以下两方面判断。
(1)如果其既满足条件1又满足条件2,则可以输出正确。
(2)如果“aPbTc”是正确的,那么也可以输出正确。那么我们如何去判断“aPbTc”正确呢?这又回到了一开始的如何判断一个字符串是“答案正确”还是“答案错误”这个原问题。递归逻辑就此形成。
时间复杂度是O(n),其中n为输入字符串的长度。空间复杂度即递归深度,而是否递归以及递归深度到底是少的则取决于输入的字符串的形式。
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解题报告: