PAT: 1003 I want to pass! (C language version)

Correct answer "is the most joy automatic reply sentence topic this question belongs to the system given the PAT." Correct answer "big delivery - just read the string following conditions are satisfied, the system will output" correct answer ", otherwise output "wrong answer."

Get the "correct answer" conditions are:

1: string must only P, A, T three character, other characters can not contain;
2: xPATx arbitrary shape such as a string can get "correct answer", or where x is an empty string, or a string is composed only of letters;
3: If aPbTc is correct, then aPbATca is correct, wherein a, b, c or all the empty string, or a string consisting only of letters a.
Now ask you to write a PAT referee program automatically determines which strings can get "the answer right".

Input format:
Each test comprises a test input. Line 1 is given a positive integer n (<10), is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.

Output formats:
the detection result row for each string, if the string can get "correct answer", the output YES, otherwise output NO.

Yang_li shu_ru:
8
PAT at
PAAT
AAPATAA
AAPAATAAAA
xPATx
the PT
Whatever the
APAAATAA

Output Sample:
YES
YES
YES
YES
NO
NO
NO
NO
. 1: string must only P, A, T three character, other characters can not contain;
2: xPATx arbitrary shape such as a string can get "answers right ", or where x is the empty string, or a string consisting only of letters a;
3: If aPbTc is correct, then aPbATca is correct, wherein a, b, c or all the empty string, or a string is composed only of letters.

This question is the key to the understanding of the meaning of the questions, this question should meet these three conditions, rather than to meet any one sister can. For 1: Note that if the character occurrence is outside the PAT answer is incorrect; 2,3 apparent from the P, T and occurs at least once at most, and appear in front of T P; at least once for A;
PAT
the PAAT
AAPATAA
AAPAATAAAA
hypothesis xPyTz
then be introduced by the law of 2,3 x * y = z.

#include <stdio.h>

int isTrue(char *a);

int main()
{
    int n, i;
    char c[10][101];
	
	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%s", c[i]);
    for (i = 0; i < n; i++)
    {
        if (isTrue(c[i]))
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;    
}
int isTrue(char *a)
{
    int i;
    int p1 = 0, p2 = 0, p3 = 0, p4 = 0;  // 判断P,T,A这三个字母是否都出现,且没有其他字母出现。

    for (i = 0; a[i] != '\0'; i++)
    {
        if (a[i] == 'P')
            p1++;                 //P出现, 且记录出现的次数。
        else if (a[i] == 'A')
            p2 = 1;                 //A出现。
        else if (a[i] == 'T')
            p3++;                 //T出现,且记录出现的次数。
        else
        {
            p4 = 1;                 //其他字母出现。
            break;
        }
    }
    if (p4) 							//其他字母出现了,NO
        return 0;                   
    if (p1 != 1 || p2 ==  0 || p3 != 1 )      //A没有出现,P,T没有出现,或者出现多次 ,则NO。
    	return 0;
    int posP = 0, posT = 0;        //第一个P和第一个T出现的位置。
    for (i = 0; a[i] != '\0'; i++)
    {
        if (a[i] == 'P')
            posP = i;
        else if (a[i] == 'T')
            posT = i;
    }
    if (posP > posT)      //T在P之前出现,则NO 
		return 0; 
    int n1 = 0, n2 = 0, n3 = 0;    //P前A的数量,P与T之间A的数量,T之后A的数量。
    for (i = 0; i < posP; i++)       
        if (a[i] == 'A')
            n1++;
    for (i = posP + 1; i < posT; i++)
        if (a[i] == 'A')
            n2++;
    for (i = posT + 1; a[i] != '\0'; i++)
        if (a[i] == 'A')
            n3++;
    if ( n1*n2 == n3) //关键
        return 1;
    
    return 0;
}
Published 24 original articles · won praise 0 · Views 159

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105046115