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
题目大意
给定一个PAT字符串,问其中存在多少个PAT组合
分析
统计字符'A'左边'P'的个数右边'T'的个数,相乘即为这个'A'的PAT组合个数,遍历所有的'A',便可以知道PAT组合的个数。
#include<iostream>
using namespace std;
int main(){
string s;
cin>>s;
long long p_cnt=0,t_cnt=0,cnt=0;
for(int i=0;i<(int)s.length();i++){
if(s[i]=='T') t_cnt++;
}
for(int i=0;i<int(s.length());i++){
if(s[i]=='P') p_cnt++;
if(s[i]=='T') t_cnt--;
if(s[i]=='A') cnt += p_cnt*t_cnt;
}
cout<<cnt%1000000007;
return 0;
}
网友评论