PATB1003 I want to pass!

insert image description here
I didn't find any rules at the beginning of this question

. Although I passed it, I wrote 80 lines of code. I looked at other people's codes and found that there are rules to follow. The number of characters 'A' in a, b, and c is represented by numa. , numb, numc, then there is numa*numb=numc, which needs to be pushed by yourself.

At the beginning, we can intuitively find that the most basic situation is xPATx, where x can be a string composed of any number of characters 'A' (of course, the length cannot exceed the range given by the title), and then look at the third item, which is actually It is a recursive formula. The most basic case is that the number of characters 'A' contained in b is one, and then the number of A in b is incremented by one each time, and we know that if b has only one A, the third In this condition, a and c must be the same, then the new string composed of ca in the aPbATca pushed back will also add a c, and the new ca will be used as the c of the next recursive formula, so in fact, Equivalent to accumulation, c will add an a each time, similar to the change of b, the difference is that b adds an A each time, and c adds an a each time, so you can get numa * numb=numc The

code is as follows

#include <iostream>
#include <string>
using namespace std;

int n;
int main()
{
    
    
    string str;
    cin>>n;
    for(int i=0;i<n;i++){
    
    

        int posP=0,posT=0;
        int countP=0,countT=0;
        bool flag=true;
        cin>>str;

        for(int j=0;j<str.size();j++){
    
    
            if(str[j]=='P'){
    
    
                countP++;
                posP=j;
            }
            else if(str[j]=='T'){
    
    
                countT++;
                posT=j;
            }
            else if(str[j]!='A'){
    
    
                flag=false;
                break;
            }
        }

        if(!flag||countP!=1||countT!=1||posT-posP<2){
    
    
            cout<<"NO"<<endl;
        }
        else{
    
    
            int numa=0,numb=0,numc=0;//a、b、c中A的个数
            numa=posP,numb=posT-posP-1,numc=str.size()-posT-1;
            if(numa*numb==numc)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389874&siteId=291194637
Recommended