PAT A1093题解
题目:
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
10^5 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.
思路,用p累计P出现次数,pa累计PA出现次数,pat累计PAT出现次数,显然pa=pa+p,pat=pat+pa,最后输出pat即可
代码:
#include<cstdio>
#include<cstring>
const int MODE = 1000000007;
char str[100010];
int main(){
scanf("%s",str);
int len = strlen(str);
int sum = 0;
int p=0,pa=0,pat=0;
for(int i=0;i<len;i++){
if(str[i]=='P')p++; //累计P出现次数
if(str[i]=='A')pa=(pa+p)%MODE; //累计PA出现次数
if(str[i]=='T')pat=(pat+pa)%MODE; //累计PAT出现次数
}
printf("%d\n",pat);
return 0;
}