有几个PAT【*】

链接:http://www.nowcoder.com/questionTerminal/5e7d025e91ab468f909cb93d431b89c3

题目描述

字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T);第二个PAT是第3位(P),第4位(A),第6位(T)。



现给定字符串,问一共可以形成多少个PAT?

输入描述:

输入只有一行,包含一个字符串,长度不超过105,只包含P、A、T三种字母。



输出描述:

在一行中输出给定字符串中包含多少个PAT。由于结果可能比较大,只输出对1000000007取余数的结果。

输入例子:

APPAPT

输出例子:

2

思路:看了别人的代码才写出来的。题目挺不错的
 1 #include "iostream"
 2 #include <iomanip>
 3 #include <string.h>
 4 #include <string>
 5 #include <vector>
 6 #include <cmath>
 7 #include <cctype>
 8 #include <algorithm>
 9 using namespace std;
10 
11 const int MAXN = 100005;
12 int cntp[MAXN];
13 int cnta[MAXN];
14 int cntt[MAXN];
15 
16 int main()
17 {
18     string str;
19     cin >>str;
20     int count = 0;
21     memset(cntp, 0, sizeof(cntp));
22     memset(cntt, 0, sizeof(cntt));
23     memset(cnta, 0, sizeof(cnta));
24     for(int i=0; i<str.length(); ++i)
25     {
26         if(i > 0)    cntp[i] = cntp[i-1];
27         if(str[i] == 'P')    ++cntp[i];
28         else if(str[i] == 'A')    cnta[i] = 1;
29     }
30     for(int i=str.length()-1; i>=0; --i)
31     {
32         cntt[i] = cntt[i+1];
33         if(str[i] == 'T')    ++cntt[i];
34     }
35     for(int i=0; i<str.length(); ++i)
36     {
37         if(cnta[i] == 1)
38         {
39             count = (count+cntp[i]*cntt[i])%1000000007;
40         }
41     }
42     cout <<count <<endl;
43     return 0;
44 }
原文地址:https://www.cnblogs.com/mtc-dyc/p/4631313.html