chenxi16

导航

PAT (Basic Level) Practice (中文)1003 我要通过!

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAXN 101
 4 int find(char a[],char c);
 5 int test(char a[]);
 6 int main(){
 7     int n,i;
 8     char a[MAXN];
 9     scanf("%d",&n);
10     for(i=1;i<=n;i++){
11         scanf("%s",a);
12         if(test(a)==1) printf("YES\n");
13         else printf("NO\n");
14     }
15     return 0;
16 }
17 int find(char a[],char c){
18     int i;
19     for(i=0;i<strlen(a);i++){
20         if(a[i]==c) return i;
21     }
22     return -1;
23 }
24 int test(char a[]){
25     int len = strlen(a),count,j,i;
26     char c;
27     int Ploc = find(a,'P'),Tloc = find(a,'T');
28     if(Ploc!=-1 && Tloc!=-1){
29         count = Tloc - Ploc-1;
30         if(count==1){
31             if(a[Ploc+1]=='A'){
32                 if(len==3) return 1;
33                 else if(len<3) return 0;
34                 else{
35                     if(Ploc==(len-Tloc-1)){
36                         for(i=0;i<Ploc;i++){
37                             c = a[len-i-1];
38                             if(c==a[i]&& a[i]=='A') ;
39                             else return 0;
40                         }
41                         return 1;
42                     }
43                     else return 0;
44                 }
45             }
46             else return 0;
47         }
48         else if(count>1){
49             char b[MAXN],c;
50             int i,end;
51             for(i=0;i<len;i++){
52                 c = a[i];
53                 if(i!=Ploc&&i!=Tloc){
54                     if(c!='A') return 0;
55                 }
56             }
57             for(i=0;i<=Ploc+1;i++) b[i] = a[i];
58             b[i] = a[Tloc];
59             if((count-1)*Ploc>=(len-Tloc-1)) b[++i] = a[len];
60             else{
61                 end = len-(count-1)*Ploc-1;
62                 for(j=Tloc+1;j<=end;j++){
63                     b[++i] = a[j];
64                 }
65                 b[++i] = a[len];
66                 
67             }
68             //printf("%s\n",b);
69             if(test(b)==1) return 1;
70                 else return 0;
71         
72         }
73         else return 0;
74     }
75     else return 0;
76 }
View Code

 

posted on 2020-04-09 16:38  chenxi16  阅读(109)  评论(0编辑  收藏  举报