1040. 有几个PAT(25)

题目描述

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

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

输入格式:

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

输出格式:

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

输入样例:

APPAPT

输出样例:

2

提交代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
int main()
{
string str;
int len, i;
int count_p = 0;
int count_t = 0;
int result = 0;

cin >> str;
len = str.length();
for (i = 0; i < len; i++) {
if (str[i] == 'T') {
count_t++;
}
}
for (i = 0; i < len; i++) {
if (str[i] == 'P') {
count_p++;
} else if (str[i] == 'T') {
count_t--;
} else if (str[i] == 'A') {
result = (result + (count_p * count_t) % 1000000007) % 1000000007;
}
}
cout << result << endl;

return 0;
}