题意:给定一个字符串,数总的PAT的个数有多少个。
思路:对于A字符而言,A前面P的个数和A后面T的个数的乘积便是该由该A所能构成的pat的个数,把每个由A组成的PAT个数总和加起来,便是所有的PAT个数。
代码:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
cin >> s;
int countT = 0,countP = 0,result = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'T')countT++;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'P') countP++;
else if (s[i] == 'T')countT--;
else if (s[i] == 'A') result = (result + (countP*countT))%1000000007;
}
printf("%d\n",result);
system("pause");
return 0;
}