PAT:1003. 我要通过!(20) AC

#include<stdio.h>
#include<string.h>

int main()
{
  int n;
  scanf("%d",&n);
  while(n--)
  {
    char str[110];
    scanf("%s",str);
    int len=strlen(str);
    int numP=0,numT=0,other=0;    //记录P,T,非P,A,T的字符个数
    int PI=-1,TI=-1;        //记录P T的位置
    for(int i=0 ; i<len ; ++i)
    {
      if(str[i]=='P')
      {
        ++numP;
        PI=i;
      }
      else if(str[i]=='T')
      {
        ++numT;
        TI=i;
      }
      else if(str[i]!='A')
        ++other;
    }
    if((other!=0) || (TI-PI<=1) || (numP!=1) || (numT!=1))    //有其他字符,PT中间没有字符,P不是唯一的,T不是唯一的,都是错的
    {
      printf("NO
");
      continue;
    }
    int x=PI,y=TI-PI-1,z=len-TI-1;    //统计P前A,P与T之间,T之后的A的个数
    if(z-x*(y-1)==x)          //【思维】回退y-1次,P与T中间只剩下1一个A,而P左边和T右边的A应该是一样多的
      printf("YES
");
    else
      printf("NO
");
  }
  return 0;
}
原文地址:https://www.cnblogs.com/Evence/p/4321453.html