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 105characters 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<stdio.h> #include<string> #include<queue> #include<algorithm> #include<string.h> #include<vector> using namespace std; int main() { string aa; cin>>aa; long long cntp,cnta,cntt; cnta=cntp=cntt=0;
long long mem=0;
long long cnt=0; for(int i=0;i<aa.size();i++) { switch(aa[i]) { case 'P':cntp++;break; //记录出现的p的总数 case 'A':cnta=(cnta+cntp)%1000000007;break; //记录下个T到来之前经过该A的T的数目 case 'T': { cnt=(cnt+mem+cnta)%1000000007; //以上个T为结尾的字符串,也同样可以以这个T结尾。再加上经过两个T之间的A的数目。 mem=(mem+cnta)%1000000007; //以当前T为结尾的PAT数目。 cnta=0; break; } } } cout<<cnt; return 0; }