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:APPAPTSample Output:
2
#include <iostream>
#include <vector>
using namespace std;
int main() {
string in;
int mode = 1000000007;
int p[100010], t[100010];
cin >> in;
for (int i = 0; i < in.size(); ++i)
{
if(i == 0) p[i] = 0;
else p[i] = p[i-1];
if(in[i] == 'P') {
p[i] += 1;
}
}
for(int i = in.size()-1; i >= 0; i--) {
if(i == in.size()-1) {
t[i] = 0;
} else {
t[i] = t[i+1];
}
if(in[i] == 'T') {
t[i] += 1;
}
}
int ans = 0;
for (int i = 0; i < in.size(); ++i)
{
if(in[i] == 'A') {
ans = ((p[i]*t[i])%mode+ans)%mode;
}
}
cout << ans << endl;
return 0;
}
本文深入探讨了一种用于计算包含特定模式(PAT)的字符串数量的算法。通过实例分析,展示了如何利用前缀数组和后缀数组来高效地解决这一问题,并提供了代码实现。该算法特别适用于处理包含字符P、A、T的输入字符串,旨在找出所有满足条件的PAT模式。通过详细解释每一步操作和关键逻辑,读者能够理解并应用这种算法解决类似问题。
492

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



