A1093 Count PAT‘s [字符串统计]

在这里插入图片描述
题目大意:统计PAT的次数
思路:先记录每个位置上P出现了多少次,再从右往左扫描记录T的次数,若扫描到A则P出现的次数*T出现的次数记得还要取余。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<time.h>
using namespace std;
const int maxn = 100001;
const int mod = 1000000007;
int leftNumP[maxn] = { 0 };
int main()
{
	string a;
	getline(cin, a);
	int n = a.length();
	for (int i = 0; i < n; i++)
	{
		if (i > 0)
			leftNumP[i] = leftNumP[i - 1];
		if (a[i] == 'P')
			leftNumP[i]++;
	}
	int ans = 0, rightNumT = 0;
	for (int j = n - 1; j >= 0; j--)
	{
		if (a[j] == 'T')
			rightNumT++;
		else if (a[j] == 'A')
			ans = (ans+leftNumP[j] * rightNumT) % mod;
	}
	cout << ans << endl;
}

posted @ 2020-07-13 15:56  _Hsiung  阅读(66)  评论(0编辑  收藏  举报