原题链接:1093 Count PAT’s (25分)
关键词:计数题
1093 Count PAT’s (25分)
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.
Now given any string, you are supposed to tell the number of PAT's contained in the string.
Input Specification:
Each input file contains one test case. For each case, there is only one line giving a string of no more than 105,characters containing only P, A, or T.
Output Specification:
For each test case, print in one line the number of PAT’s contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.
Sample Input:
APPAPT
Sample Output:
2
题目大意: 给出一个由P、A、T三个字母组成的字符串,问你在这之中能构成多少个PAT子串 。
思路:
- 对于字符串中的每一个
A,它能够构成字串PA的数量是它之前的所有P的数量。 - 对于字符串中的每一个
T,它能够构成字串PAT的数量是它之前所有字串PA的数量。
代码:
#include <iostream>
using namespace std;
const int mod = 1e9 + 7;
long long p, a, b, res;
int main(){
string str;
cin >> str;
for (auto c : str)
if (c == 'P') p ++;
else if (c == 'A') b += p;
else res = (res + b) % mod;
cout << res << endl;
}
本文介绍了一种高效算法,用于计算给定字符串中包含的特定子串“PAT”的数量。通过统计每个'A'前的'P'和每个'T'前的'PA'组合,巧妙地解决了可能存在的大量重复计算问题。

1753

被折叠的 条评论
为什么被折叠?



