1. 程式人生 > >1040 有幾個PAT (25 分)

1040 有幾個PAT (25 分)

字串 APPAPT 中包含了兩個單詞 PAT,其中第一個 PAT 是第 2 位§,第 4 位(A),第 6 位(T);第二個 PAT 是第 3 位§,第 4 位(A),第 6 位(T)。

現給定字串,問一共可以形成多少個 PAT?

輸入格式:

輸入只有一行,包含一個字串,長度不超過10​5​​,只包含 P、A、T 三種字母。

輸出格式:

在一行中輸出給定字串中包含多少個 PAT。由於結果可能比較大,只輸出對 1000000007 取餘數的結果。

輸入樣例:

APPAPT

輸出樣例:

2
#include <bits/stdc++.h>
using
namespace std; #define m 1000000007 int main() { string s; cin>>s; int P=0,PA=0,PAT=0; for(int i=0; i<s.length(); i++) { if(s[i]=='P') { P++; } else if(s[i]=='A') { PA=(PA+P)%m; } else if(s[i]=='T') {
PAT=(PAT+PA)%m; } } cout<<PAT<<endl; }