PTA 1003 I want to pass! (C language)

" Right answer " is the most joy automatic reply sentence topic given system. This title belongs to the PAT of "correct answer" big delivery - just read the string following conditions are satisfied, the system will output " right answer ", otherwise outputs " Wrong Answer ."

Get the " right answer " conditions are:

String must have only P、 A、 Tthree characters, it must not contain other characters;

  1. Arbitrary shape such as xPATxa string can get "correct answer", wherein xeither the empty string, or a string consisting only of letters A;
  2. If you aPbTcare correct, then aPbATca is correct, which a, b, care either empty string, or only by the letter Astring composed.
  3. Now ask you to write a PAT referee program automatically determines which strings can get " correct answer " is.

Input formats:

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.

Sample input:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

Analysis: conditions may answer given condition is not clear, but you can see the input and output sample according to the following: First, you want to correct Pand Tcertainly have and the only one in PATcan be inserted in any position A, but which also have certain rules. Since then APAAATAAis wrong, AAPATAAis correct, the analysis for a long time, then looked at a sister school of the articles found, must be
Pbefore Athe number * Pand Tthe number of A between the = Tfollowing Anumber of
detailed analysis of the following links:
poke here

After clarify this issue resolved just fine.

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    char str[101];
    scanf("%d",&n);
    int i,j;

    for(i=0;i<n;i++){
	    int a=0;//记录A的数目 
	    int p=0;//P的个数 
	    int t=0;//T的个数 
	    int lp,lt;//P和T的位置 
        int flag=1;//循环控制 
        scanf("%s",str);
        for(j=0;str[j]!='\0';j++){
            if(str[j]=='A'){
                a++;
        	}
            else if(str[j]=='T'){
                t++;
                lt=j;
            }
            else if (str[j]=='P'){
                p++;
                lp=j;
            }
            else {
            	flag=0;
            	break;
			}
            
        }
        if(p==1 && t==1 && a!=0 && (lt-lp)!=1 && lp*(lt-lp-1)==strlen(str)-lt-1 ){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    }
    return 0;
}
Released five original articles · won praise 0 · Views 59

Guess you like

Origin blog.csdn.net/Czrobe/article/details/104880502