#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int len = s.length();
int result = 0;
int countp = 0;
int countt = 0;
for (int i = 0; i < len; i++) {
if (s[i] == 'T')
countt++;
}
for (int i = 0; i < len; i++) {
if (s[i] == 'P') countp++;
if (s[i] == 'T') countt--;
if (s[i] == 'A') {
result = (result + (countp * countt) % 1000000007) % 1000000007; //如果不取余的话 就会有几个测试点出错
}
}
cout << result;
return 0;
}
/*分析:要想知道构成多少个PAT,
那么遍历字符串后对于每一A,它前
面的P的个数和它后面的T的个数的乘积
就是能构成的PAT的个数。然后把对于
每一个A的结果相加即可~~辣么就简单
啦,只需要先遍历字符串数一数有多少
个T~~然后每遇到一个T呢~countt–;每
遇到一个P呢,countp++;然后一遇到字母
A呢就countt * countp~~~把这个结果累加到re
sult中~~~~最后输出结果就好啦~~对了别忘
记要对10000000007取余哦~~~~
为什么要对1000000007取模
大数阶乘,大数的排列组合等,一般都要求将输出结果对1000000007取模
为什么总是1000000007呢= =
大概????是因为:(我猜的,不服你打我呀~)
1. 1000000007是一个质数
2. int32位的最大值为2147483647,所以对于int32位
来说1000000007足够大
3. int64位的最大值为2^63-1,对于1000000
007来说它的平方不会在int64中溢出
所以在大数相乘的时候,因为(a?b)%c=((a%c)?(b%c))%c,所以相乘时两
边都对1000000007取模,再保存在int64里面不会溢出
*/
PAT basic 1040
最新推荐文章于 2020-07-26 11:37:51 发布