1. 程式人生 > >PAT-BASIC1003——我要通過

PAT-BASIC1003——我要通過

題目描述:

知識點:遞迴

思路:條件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解題報告: