Count PAT's
题目描述:
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.
输出描述:
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
输出样例:
2
本人AC代码:
#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
getline(cin,s);
int n=s.size();
vector<long long> sb(n,0);
long long sum=0;
long long ans=0;
for(int i=0;i<n;i++){
if(s[i]=='P'){
sum++;
}
else if(s[i]=='A'){
sb[i]=sum;
}
}
sum=0;
for(int i=n-1;i>=0;i--){
if(s[i]=='T'){
sum++;
}
else if(s[i]=='A'){
sb[i]*=sum;
sb[i]%=mod;
}
}
for(int i=0;i<n;i++){
ans+=sb[i];
ans%=mod;
}
cout<<ans<<endl;
return 0;
}
声明:不保证代码健壮性,只确保通过顶部链接练习网站所提供测试样例;