1093 Count PAT's (25分)

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的个数

解题思路:动态规划的思想。

分析一个测试用例:APPAPAAT,输出8

A P P A P A A T
P数组 0 1 2 2 3 3 3 3
PA数组 0 0 0 0+2=2 2 2+3=5 5+3=8 8
PAT个数 0 0 0 0 0 0 0 0+8=8

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int p[maxn], pa[maxn];
int main()
{
	int c = 0, tp = 0, tpa = 0, ans = 0;
	string s;
	cin >> s;
	int l = s.length();
	for(int i=0; i<l; i++){
		if(s[i]=='P') p[i] = tp++;
		p[i] = tp;
		
		if(s[i]=='A') tpa += p[i];
		pa[i] = tpa;
		
		if(s[i]=='T'){
			ans += pa[i];
			ans %= 1000000007;
		}
	}
	printf("%d", ans);
	return 0;
}