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
Process
找到每个位置的前缀P的总个数和后缀T的总个数,再找到每个A,计算其前缀和后缀的乘积与1000000007的求余即可。
Code
#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
string str;
int p[maxn], t[maxn], sum = 0;
int main()
{
cin >> str;
for (int i = 0; i < str.size(); i++)
{
if (i != 0)
p[i] = p[i - 1];
if (str[i] == 'P')
p[i]++;
}
for (int i = str.size() - 1; i > 0; i--)
{
if (i != str.size() - 1)
t[i] = t[i + 1];
if (str[i] == 'T')
t[i]++;
else
if (str[i] == 'A' && i != str.size() - 1 && i != 0)
sum = (sum + p[i - 1] * t[i + 1]) % 1000000007;
}
cout << sum;
return 0;
}