PAT A1093-Count PAT's

PAT A1093-Count PAT’s

topic

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 105 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

The number of strings to the effect that the number of PAT, pay special attention to the last sentencehave to output the result moded by 1000000007First I ignored, so the result is not% 1,000,000,007, looking for a long wrong, careless.

Problem-solving ideas

In Aa fixed-point, determining Athe left Pnumber, and the Adigits to the right of T, it can then be determined, in one Aposition, may be combined into a PATnumber of strings.

Code

#include <stdio.h>
#include <string.h>

using namespace std;

int main(){
    char c[1000001];
    scanf("%s", c);
    
    unsigned int res = 0, len, P = 0, T = 0;
    len = (unsigned int)strlen(c);
    for(int i = 0; i < len; i++){
        if(c[i] == 'T') T++;
    }
    for(int i = 0; i < len-1; i++){
        if(c[i] != 'A'){
            if(c[i] == 'P') P++;
            else if (c[i] == 'T') T--;
            continue;
        }
        res = (res + P * T)% 1000000007;
    }
    printf("%d\n", res);
    return 0;
}

Released nine original articles · won praise 1 · views 184

Guess you like

Origin blog.csdn.net/weixin_41515197/article/details/104121451
PAT