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
题意:给出一个只包含‘P’,‘A’,‘T’的字符串,让你统计其中‘PAT’一共多少个,其中p和t可以重复使用,并把结果对给定质数取余。
思路: 从左往右数出所有P的个数,再从右往左数出所有T的个数,分别保存在数组中,然后 在对数组中每个字符‘A’,令左边的P和右边T的个数相乘,最后累计的总个数即为所有PAT个数。
参考代码:
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int A[100010],T[100010];
int main()
{
string s;
cin>>s;
long long cnt=0;
for(int i=0;i<s.size();i++){
if(i==0&&s[i]=='P') A[i]=1;
else if(s[i]=='P') A[i]=A[i-1]+1;
else A[i]=A[i-1];
}
for(int i=s.size()-1;i>0;i--){
if(i==s.size()-1&&s[i]=='T') T[i]=1;
else if(s[i]=='T') T[i]=T[i+1]+1;
else T[i]=T[i+1];
}
for(int i=0;i<s.size();i++)
if(s[i]=='A') cnt=(cnt+A[i]*T[i])%1000000007;
printf("%d\n",cnt);
return 0;
}