题目网址
https://pintia.cn/problem-sets/994805260223102976/problems/994805282389999616
思路
有点动规的意思,PA出现的次数等于之前PA出现的次数加上当前P的次数(当前字符为’A’时),同理PAT出现的次数等于之前PAT出现的次数加上当前AP出现的次数(当前字符为’T’时)。具体看代码理解。
代码
#include<iostream>
#define theNum 1000000007
using namespace std;
int main() {
string s;
int len;
long P = 0;
long PA = 0;
long PAT = 0;
cin >> s;
len = s.length();
for (int i = 0; i < len; i ++) {
if (s[i] == 'P')
P++;
if (s[i] == 'A')
PA += P;
if (s[i] == 'T')
PAT = (PAT + PA) % theNum;
}
cout << PAT << endl;
return 0;
}