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


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
const int MOD = 1000000007;
char str[MAXN];
int leftNumP[MAXN] = { 0 };
int main()
{
/*int count = 0;
string s;
cin >> s;
for (int pos1 = s.find('P'); pos1>= 0 && pos1 <= s.size() - 3; pos1 = s.find('P', pos1+1))
{
for (int pos2 = s.find('A',pos1+1); pos2 >= 0 && pos2 <= s.size() - 2; pos2 = s.find('A', pos2+1))
{
for (int pos3 = s.find('T', pos2 + 1); pos3 >= 0 && pos3 <= s.size() - 1;pos3 = s.find('T', pos3+1))
{
count++;
}
}
}
cout << count% 1000000007;
暴力法超时*/
gets_s(str);//读入字符串
int len = strlen(str);//长度
for (int i = 0; i < len; i++)
{//从左到右遍历字符串
if (i > 0)
{//如果不是0号位
leftNumP[i] = leftNumP[i - 1];//继承上一位的结果
}
if (str[i] == 'P')
{//当前位是P
leftNumP[i]++;//令leftNumP[i]+1
}
}
int ans = 0, rightNumT = 0;//ans为答案,rightNumT记录右边T的个数
for (int i = len - 1; i >= 0; i--)//从右到左遍历字符串
{
if (str[i] == 'T')//当前位是T
rightNumT++;//右边T的个数加1
else if (str[i] == 'A')//当前位是A
ans = (ans + leftNumP[i] * rightNumT) % MOD;//累积乘积

}
printf("%d", ans);
return 0;
}//提交时gets_s改为gets即可