The string appapt contains two words "Pat", where the first Pat is 2nd (P), 4th (a), 6th (t), and the second Pat is 3rd (p), 4th bit (a), 6th (t).
Now given a string, how many pat can it form?
Input format:
Input has only one row, contains a string, the length of not more than 105, only contains P, A, T three kinds of letters.
Output format:
Outputs the number of PAT contained in a given string in a row. Because the result may be larger, only the result of the remainder is output to 1000000007.
Input Sample:
Appapt
Sample output:
2
The core idea is to count the number of p on the left side of each character a and how many t on the right.
In order to reduce the number of traversal times, we first traverse to get the number of T in the entire string
The second time from left to right traversal of the string, met P nump++, representing the next a before the number of p, met T numt--, on behalf of the next a how many T.
Finally, take a look at ll and mod on it.
#include <bits/stdc++.h>#defineLL Long Long#defineMAXN 100000+50#defineMOD 1000000007using namespacestd;CharSTR[MAXN]; LL Res=0; LL Nump=0, NUMT =0;intMain () {scanf ("%s", str); intLen =strlen (str); for(inti =0; i < Len; ++i) { if(Str[i] = ='T') {numt++; } } for(inti =0; i < Len; ++i) { if(Str[i] = ='P') {Nump++; } Else if(Str[i] = ='A') {res= (RES+NUMP*NUMT)%MOD; } Else if(Str[i] = ='T') {numt--; }} cout<< Res <<Endl; return 0;}Capouis ' CODE
Pat-basic-1040-has a few pat