PAT 1093. Count PAT's

1093. Count PAT's (25)

时间限制
120 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.

Now given any string, you are supposed to tell the number of PAT's contained in the string.

Input Specification:

Each input file contains one test case. For each case, there is only one line giving a string of no more than 105 characters containing only P, A, or T.

Output Specification:

For each test case, print in one line the number of PAT's contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

Sample Input:
APPAPT
Sample Output:
2

题目解析:
计算PAT按顺序排列的字符串个数。直接遍历会超时。从最右端开始遍历,统计该位置为止T的个数。若出现A则该位置为止AT组合的个数为该位置右侧AT组合的个数加上该位置右侧T的个数。若出现P,则总数加上该位置右侧AT组合个数。
 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int main(void){
 5     char test[100005];
 6     int a[100005], t[100005];
 7     char aim[4] = "PAT";
 8     long long num = 0;
 9     long long i, j, k;
10     scanf("%s", test);
11     long long len = strlen(test);
12     a[len] = 0;
13     t[len] = 0;
14     for (i = len - 1; i >= 0; i--){
15         if (test[i] == 'T'){
16             t[i] = t[i + 1] + 1;
17         }
18         else{
19             t[i] = t[i + 1];
20         }
21         if (test[i] == 'A'){
22             a[i] = a[i + 1] + t[i + 1];
23         }
24         else{
25             a[i] = a[i + 1];
26         }
27         if (test[i] == 'P'){
28             num += a[i + 1];
29         }
30     }
31     printf("%d\n", num % 1000000007);
32     return 0;
33 }

 

posted @ 2018-01-21 18:04  GrayWind  阅读(78)  评论(0)    收藏  举报