判断字符串格式正误

问题描述:

 问题分析:

题目理解:首先****PAT****正确,左侧*和右侧*一样多,且如果有*,只能是A
其次 如果每次往P和T中间加一个A,则T后面增加P前面的*数量,通过递推可知:
T后面的*数量是P前面的P和T之间的A的个数倍

代码展示:

 1 //题目理解:首先****PAT****正确,左侧*和右侧*一样多,且如果有*,只能是A
 2 //其次 如果每次往P和T中间加一个A,则T后面增加P前面的*数量,通过递推可知:
 3 //T后面的*数量是P前面的P和T之间的A的个数倍
 4 #include<stdio.h>
 5 #include<string.h>
 6 int main()
 7 {
 8     void judge(char *);
 9     int n;
10     char c[101];
11     char *p=c;
12     scanf("%d",&n);
13     for(int i=0;i<n;i++)
14     {
15         scanf("%s",p);
16         judge(p);
17     }
18     return 0;
19 }
20 
21 void judge(char *p)
22 {
23     int n=strlen(p);
24     int n1=0,n2=0,n3=0;
25     int countp=0,counta=0,countt=0;
26     int posp,post;
27     char *p1;
28     p1=p;
29     
30     int i;
31     for(i=0;i<n;i++)
32     {
33         if(*(p1+i)!='P'&&*(p1+i)!='A'&&*(p1+i)!='T')
34         {
35             printf("NO
");
36             return;
37         }
38         if(*(p1+i)=='P') countp++;
39         else if(*(p1+i)=='A') counta++;
40         else countt++;
41     }
42     if(countp!=1||counta<1||countt!=1)
43     {
44             printf("NO
");
45             return;
46     }
47     
48     for(i=0;i<n;i++)
49     {
50         if(*(p1+i)=='P') posp=i;
51         if(*(p1+i)=='T') post=i;
52     }
53     if(posp>post)
54     {
55         printf("NO
");
56         return;
57     }
58     for(i=0;i<n;i++)
59     {
60         if(*(p1+i)=='A')
61         {
62             if(i<posp) n1++;
63             else if(posp<i&&i<post) n2++;
64             else n3++;
65         }
66     }
67     if(n3!=n2*n1)
68     {
69         printf("NO
");
70         return;
71     }
72     printf("YES
");
73 }

结果:

原文地址:https://www.cnblogs.com/bboykaku/p/12585037.html