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;
}
这篇博客介绍了如何使用C++解决一个字符串问题,即找出含有两个特定子串PAT的计数问题。通过逐字符累加P、PA和PAT的出现次数,最后输出PAT子串的总数,结果取模10^9+7。
1314

被折叠的 条评论
为什么被折叠?



