【题意】
找出给出的字符串中PAT子串的个数
【思路】
对于每一个A,PA的个数由前面P的个数决定;对每一个T,PAT的个数由前面PA的个数决定,所以只需要从前往后扫一遍,记下中间P的累计个数、PA的累计个数即可得到答案
#include <iostream>
#include <string>
using namespace std;
#define SIZE 1000000007
int main(){
string str;
cin >> str;
int pCnt, aCnt, total;
pCnt = aCnt = total = 0;
for (int i = 0; i < str.length(); ++i){
switch (str[i]){
case 'P':
++pCnt;
pCnt %= SIZE;
break;
case 'A':
aCnt += pCnt;
aCnt %= SIZE;
break;
default:
total += aCnt;
total %= SIZE;
break;
}
}
cout << total;
system("pause");
return 0;
}