Several PAT 1040 (25 minutes)
contains two word strings APPAPT PAT in which the first two are the PAT §, bit 4 (A), bit 6 (T); second PAT is of 3 §, bit 4 (A), bit 6 (T).
Now given string, ask how many total PAT can be formed?
Input format:
input single line comprising a string of not more than 10 ^ 5
, comprising only P, A, T three kinds of letters.
Output format:
output line number to a given string contains PAT. As a result may be relatively large, only the output of 1,000,000,007 take the results of the remainder.
Sample input:
APPAPT
Sample output:
2
Problem-solving ideas: first count the number of each corresponding P, then start traversing from the last position, the number of T statistics appear, when confronted with a A, then count the number of the product of the number of P and T Finally, to modulo
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 100005;
char str[MAXN];
int p[MAXN] = { 0 };
int main() {
scanf("%s",str);
int length = strlen(str);
int countT = 0;
int total = 0;
for (int i = 0; i < length; i++){
if (i>0){
p[i] = p[i - 1];
}
if (str[i] == 'P'){
p[i]++;
}
}
for (int i = length-1; i >=0; i--){
if (str[i] == 'T'){
countT++;
}
if (str[i] == 'A'){
total = (total+p[i]*countT) % 1000000007;
}
}
printf("%d\n", total);
return 0;
}