PAT (Basic Level) Practise:1040. 有几个PAT

【题目链接】

字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T);第二个PAT是第3位(P),第4位(A),第6位(T)。

现给定字符串,问一共可以形成多少个PAT?

输入格式:

输入只有一行,包含一个字符串,长度不超过105,只包含P、A、T三种字母。

输出格式:

在一行中输出给定字符串中包含多少个PAT。由于结果可能比较大,只输出对1000000007取余数的结果。

输入样例:

APPAPT

输出样例:

2

提交代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void test(void)
 5 {
 6 #define MAX_STR_LEN     (100000)
 7     char str[MAX_STR_LEN+1];
 8     int len;
 9     int i, j, k;
10     unsigned int cnt = 0;
11     unsigned int cntP, cntA, cntT;
12 
13     cntP = cntA = cntT = 0;
14 
15 //    printf("input str:
");
16     memset(str, 0x00, sizeof(str));
17     scanf("%s", str);
18     len = strlen(str);
19     for(i = 0; i < len; i++)
20     {
21         if(str[i] == 'P')
22         {
23             for(j = i+1; j < len; j++)
24             {
25                 if(str[j] == 'A')
26                 {
27                     for(k = j+1; k < len; k++)
28                     {
29                         if(str[k] == 'T')
30                         {
31                             cnt++;
32                             if(cnt >= 1000000007)
33                                 cnt = 0;
34                         }
35                     }
36                 }
37             }
38         }
39     }
40     printf("%d", cnt);
41 }
42 
43 int main(void)
44 {
45     test();
46     return 0;
47 }

运行结果:

循环太多,明显要超时了,囧。。。

原文地址:https://www.cnblogs.com/utank/p/4470684.html