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<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
using namespace std;
int main(){
freopen("F://Temp/input.txt", "r", stdin);
string input;
cin >> input;
int P = 0, PA = 0, PAT = 0;
for (int i = 0; i < input.size(); ++i){
if (input[i] == 'P')
P++;
else if (input[i] == 'A')
PA += P;
else if (input[i] == 'T')
PAT += PA;
if (PAT > 1000000007)
PAT %= 1000000007;
}
cout << PAT << endl;
return 0;
}
本文介绍了一种高效计算给定字符串中PAT子串数量的方法,通过O(n)复杂度实现,适用于长度不超过10^5的字符,仅包含P、A、T三种字符的字符串。提供详细代码及解析,帮助理解算法核心。
482

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



