[PAT Basic Level] B by 1003. I want to

Topic analysis:

This question, quite helpless, that is difficult is hard to say simple and simple, just beginning to understand the situation got wrong WA, later noted that the last subject to a test case, and realize that I was wrong back to Italy. The key to the solution of this question is to understand the meaning of the title really want to express, and feel a little test reading comprehension careful thought.
Here Insert Picture Description
Topic focuses on how to understand the second and third conditions:

  • For the second condition is to be noted: x is a string, so xPATx, it means that the left and right sides of the PAT string must be the same, or empty string, i.e. there is no, or are composed of string A, then In addition to determining whether the length is not equal to the outside it is needed A determination.
  • For the third condition, but also more refined look. If aPbTc is correct, you can continue to launch aPbATca delivery also correct, then it is important to determine the starting point of this recurrence relation, and little thought will be able to understand that this situation is xPATx starting point, that is the case meet two conditions. Thus, the third method of determining compliance is actually derived from the basis of the second condition is satisfied based on. Each recursive, multi be inserted between P and A a T, corresponding to the requirements of a multi-T string a string.

With S t r i n g A , S t r i n g B , S t r i n g C String, StringB, StringC represent a, b, c string, S t r i n g L e f t , S t r i n g M i d , S t r i n g R i g h t StringLeft,StringMid, StringRight denote string P on the left, and the string after the string between T P, T, then the equation can be obtained:
S t r i n g R i g h t = S t r i n g C + ( L e n ( S t r i n g M i d ) 1 ) S t r i n g A StringRight=StringC+(Len(StringMid)-1)*StringA
and the initial conditions require S t r i n g A = S t r i n g C String = StringC
so L e n ( S t r i n g R i g h t ) = L e n ( S t r i n g M i d ) L e n ( S t r i n g L e f t ) Len (StringRight) = Len (StringMid) * only (StringLeft)

It can be implemented with the following code (already AC)

Source

#include <stdio.h>

bool isPAT(int *ch,int len);
int main()
{
    int caseNumber;
    scanf("%d",&caseNumber);
    getchar();
    while(caseNumber--){
        int ch[100];
        int count=0;
        while((ch[count++]=getchar())!=EOF&&ch[count-1]!=10)  //读取字符,10对应换行符
        ;
        if(isPAT(ch,count-1)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
bool isPAT(int *ch,int len)
{
    int i=0;
    int left=0,mid=0,right=0;  //分别记录P前遇到,P与T之间以及T之后的A个数
    int symP=(int)'P',symA=(int)'A',symT=(int)'T';
    while(ch[i]!=symP){  //循环 直到遇到P
        if(ch[i]!=symA) return false;
        if(i++>len-4) return false; //倒数第三个字符还未遇到P,不能产生PAT
        left++;  //对P左边的A个数计数
    }

    if(ch[++i]!=symA) return false;  //P之后不是A,返回错误
    mid++;
    while(ch[++i]!=symT){   
        if(ch[i]!=symA) return false;
        if(i>len-2) return false;   //数组末尾还未遇到T,返回错误
        mid++;
    }
    
    while(++i<len){  //若后面还有数字
        if(ch[i]!=symA) return false;
        right++;  //对T后面的A计数
    }
    if(mid==1){
        if(left!=right) return false; //对于xPATx型,若两端A个数不等,返回错误
    } 
    else if(right!=mid*left) return false;
 
    return true;
}
Published 11 original articles · won praise 1 · views 77

Guess you like

Origin blog.csdn.net/weixin_44452361/article/details/104585048