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
最后两个点试了好多次都没过,原来是每次算了res后都得求余,有点坑
代码如下:
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define mod 1000000007;
int cntp[100010] = { 0 }, cntt[100010] = { 0 };
int res = 0;
string str;
int main() {
cin >> str;
int n = str.size();
int cnt = 0;
for (int i = 0; i < n; i++) {
if (str[i] == 'P') {
cnt++;
}
if (str[i] == 'A') {
cntp[i] = cnt;
}
}
cnt = 0;
for (int i = n - 1; i >= 0; i--) {
if (str[i] == 'T') {
cnt++;
}
if (str[i] == 'A') cntt[i] = cnt;
}
for (int i = 0; i < n; i++) {
if (cntp[i] != 0) {
res += (cntt[i] * cntp[i]) % mod;
res %= mod;
}
}
printf("%d", res);
return 0;
}