Skip to content

1093 Count PAT's

Statement

Metadata

  • 作者: CAO, Peng
  • 单位: Google
  • 代码长度限制: 16 KB
  • 时间限制: 150 ms
  • 内存限制: 64 MB

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 10^5 characters containing only P, A, 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

Solution

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
char s[N];
int f[110];

int main() {
    while (scanf("%s", s + 1) != EOF) {
        memset(f, 0, sizeof f);
        for (int i = 1; s[i]; ++i) {
            if (s[i] == 'P') {
                ++f['P'];
            } else if (s[i] == 'A') {
                f['A'] += f['P'];
            } else {
                f['T'] += f['A'];
            }
            f[s[i]] %= mod;
        }
        printf("%d\n", f['T']);
    }
    return 0;
}

Last update: May 4, 2022
Back to top