题目:
PAT甲级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 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
分析
题目意思大概是给出一个字符串,算出有多少个PAT,可以不连续,但是需要按照先后顺序。
思路
以A为核心,计算每个A能组成的PAT数量,即
该A前P的数量*该A后T的数量
AC代码
#include<cstdio>
using namespace std;
char in[100000];
const int max = 1000000007;
int main() {
char check;
int n = 0;
while ((check = getchar())!='\n') {
in[n++] = check;
}
int i,j,k,p=0,a=0,t=0,count=0;
for (i = 0; i < n; i++) {
if (in[i] == 'T')
t++;
}
for (i = 0; i < n; i++) {
if (in[i] == 'P')
p++;
if (in[i] == 'T')
t--;
if (in[i] == 'A') {
count = (count + (p*t)%max)%max;
}
}
printf("%d", count);
}