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
1
0
5
10^5
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
Caution:
刚开始本来想用 DFS 做,但是时间超了,后来发现其实只要一次遍历就行了。
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-08-24 12:27:00
// All rights reserved.
#include <iostream>
#include <vector>
using namespace std;
int main(){
string s;
long long ans = 0;
int lastp = 0, lasta = 0;
cin >> s;
for(int i = 0; i < s.length(); ++i){
if(s[i] == 'P') lastp++;
else if(s[i] == 'A') lasta += lastp;
else ans += lasta;
}
printf("%d\n", (int)(ans % 1000000007));
return 0;
}