PAT B1003 I want to pass! (20 points)

Insert picture description here

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main(){
    
    
	int n;
	scanf("%d", &n);
	
	string str;
	int left, mid, right;
	int flag;
	for(int i=0; i<n; i++){
    
    
		flag = 0;
		cin >> str;
		for(int j=0; j<str.length(); j++){
    
    
			if(str[j]!='P' && str[j]!='A' && str[j]!='T'){
    
    
				printf("NO\n");
				flag = 1;
				break;
			}
		}
		if(flag){
    
    
			continue;
		}
		
		left = str.find("P");
		mid = str.find("T") - str.find("P") - 1;
		right = str.length() - str.find("T") - 1;
		
		if(mid == 1){
    
    
			if(left == right){
    
    
				printf("YES\n");
			}else{
    
    
				printf("NO\n");
			}
		}else if(mid == 0){
    
    
			printf("NO\n");
		}else{
    
    
			while(mid != 1){
    
    
				mid--;
				right -= left;
			}
			if(left == right){
    
    
				printf("YES\n");
			}else{
    
    
				printf("NO\n");
			}
		}
	}
	
	return 0;
} 

I feel more and more that the topic of PAT is ambiguous. It may be that I think too much. It took me ten minutes to understand the meaning of the topic.
It is a simple simulation question. First traverse the string once. If there is a character other than the three letters of PAT, it will directly output NO and start the next input.
According to the question, it can be found that if there is only one A in the middle of the PT, the number of A on the left of P and the right of T must be equal to be correct. If there is no A in the middle of PT, it is wrong.
The third condition is more troublesome. When the number of A in the middle of the PT is greater than one, you can recurse forward according to the condition of the question until the number of A in the middle of the PT is one, and then judge whether it is correct at this time.

Guess you like

Origin blog.csdn.net/weixin_45964844/article/details/112169198
Recommended