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 A
a fixed-point, determining A
the left P
number, and the A
digits to the right of T, it can then be determined, in one A
position, may be combined into a PAT
number 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;
}