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.
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.
APPAPT
Sample Output:
2
以A为中心,统计每个A前后P和T的个数即可
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
long long P[maxn],T[maxn],p=0,t=0;
int main(){
// freopen("in.txt","r",stdin);
string str;
cin>>str;
for(int i=0;i<str.length();i++){
if(str[i]=='P') p++;//'P'不要写成p
else if(str[i]=='A') P[i]=p;//记录每个A前面有多少个'P'
}
long long ans=0;
for(int i=str.length()-1;i>=0;i--){//从后往前找'T'
if(str[i]=='T') t++;
else if(str[i]=='A'){//此时就可以计算以此A为中心的'PAT'有多少个了
ans=(ans+P[i]*t)%1000000007;
}
}
cout<<ans;
return 0;
}
|