1093. Count PAT's (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1093
题目:

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

分析:
要用O(n),不然会有例子过不了。很经典的题目;
AC代码:
#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;
}


截图:
1093. Count PAT's (25)_第1张图片
——Apie陈小旭

你可能感兴趣的:(1093. Count PAT's (25))