PAT Level B-there are several PAT

Description title
string APPAPTcontains two words PAT,

  • The first PATis the first 2position P, the first 4position A, the first 6position T;
  • The second PATis the first 3place P, first 4place A, first 6place T.

Given a string, how many can be formed in total PAT?

Input format
input single line comprising a string of not more than 10 . 5 , contains only P, A, Tthree kinds of letters.

Output format
Output how many characters are contained in a given string in one line PAT.

Since the result may be relatively large, only the result of taking the remainder of 1000000007 is output.

Enter the sample
APPAPT

Sample output
2


Problem solution
mathematics:

解题思路

  1. For each A, it can be configured PAa number of which depends on all the previous Pnumber.
  2. For each T, it can be configured PATa number of which depends on all the previous PAnumber.
#include <iostream>
using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;

int main()
{
    
    
    string s;
    cin >> s;
    
    LL P = 0, PA = 0, PAT = 0;
    for (int i = 0; i < s.size(); i ++)
        if(s[i] == 'P') P ++;
        else if(s[i] == 'A') PA += P;
        else PAT = (PAT + PA) % mod;
    
    cout << PAT << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/113861263