PAT甲级——A1093 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 1 characters containing only PA, 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

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     string input;
 8     getline(cin,input);
 9     int Pnum[input.size()]={0};//Pnum数组表示该字符左侧字符P的数量
10     for(int i=1;i<input.size();++i)
11     {//如果i-1位置是P字符,则Pnum[i]=Pnum[i-1]+1;否则Pnum[i]=Pnum[i-1]
12         Pnum[i]=Pnum[i-1];
13         if(input[i-1]=='P')
14             ++Pnum[i];
15     }
16     int tnum=0,sumPAT=0;//tnum表示该字符右侧字符T的数量,sumPAT表示PAT字符串总数
17     for(int i=input.size()-1;i>=0;--i)
18         if(input[i]=='T')
19             ++tnum;
20         else if(input[i]=='A')
21             sumPAT=(sumPAT+Pnum[i]*tnum)%1000000007;
22     printf("%d
",sumPAT);
23     return 0;
24 }
 
原文地址:https://www.cnblogs.com/zzw1024/p/11349323.html