PAT (Advanced Level) Practise 1093 Count PAT's (25)

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,直接算出每个A左边的P数量和右边的T数量相乘即可。

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int L[maxn], R[maxn];
int ans;
char s[maxn];

int main()
{
	scanf("%s", s + 1);
	for (int i = 1; s[i]; i++)
	{
		L[i] = L[i - 1] + (s[i] == 'P');
	}
	for (int i = strlen(s + 1); i; i--)
	{
		R[i] = R[i + 1] + (s[i] == 'T');
	}
	for (int i = 1; s[i]; i++)
	{
		if (s[i] == 'A') (ans += (LL)L[i - 1] * R[i + 1] % mod) %= mod;
	}
	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(pat)