问题描述:
字符串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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 100005;
string s;
typedef long long LL;
LL mod = 1000000007;
int main()
{
int num_T = 0, num_AT = 0, num_PAT = 0;
cin >> s;
for(int i = s.length() - 1; i >= 0; i --) {
if(s[i] == 'T')
num_T ++;
else if(s[i] == 'A') {
num_AT = (num_AT + num_T) % mod;
}
else {
num_PAT = (num_PAT + num_AT) % mod;
}
}
cout << num_PAT << endl;
return 0;
}