PAT甲级--1093 Count PAT's(25 分)【逻辑题】
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
解题思路:我们先统计出整个字符串中T的数量,然后我们从0开始循环,遇到P就p的数量加1,如果遇到A就P的数量乘以T的数量,如果遇到的是T就减1,就是减去出现在A前面的T减掉,在做的过程中一定要记得取余,我一开始只对乘的取余,没有把当时的结果整体取余,然后2个测试点没过,以后要谨记,不要写成+=(遇到取余的时候)
#include<bits/stdc++.h>
using namespace std;
int mod= 1000000007;
int main(void)
{
string s;
cin>>s;
int len=s.length();
int cntt=0;
for(int i=0;i<len;i++)
{
if(s[i]=='T') cntt++;
}
int cntp=0,ans=0;
for(int i=0;i<len;i++)
{
if(s[i]=='P') cntp++;
else if(s[i]=='A') ans=(ans+(cntp*cntt)%mod)%mod;
else cntt--;
}
printf("%d\n",ans);
return 0;
}