PAT 1040 有几个PAT (25 分)

#include 
#include 
using namespace std;
int main() {
    string s;
    cin >> s;
    int len = s.length(), result = 0, count_p = 0, count_t = 0;
    for (int i = 0; i < len; i++) {
        if (s[i] == 'T')
            count_t++;
    }
    for (int i = 0; i < len; i++) {
        if (s[i] == 'P') count_p++;
        if (s[i] == 'T') count_t--;
        if (s[i] == 'A') 
        result = (result + (count_p * count_t) % 1000000007) % 1000000007;
    }
    cout << result;
    return 0;
}

方法2

#include 
#define LIM 1000000007
int main()
{
    int P = 0, PA = 0, PAT = 0;
    char c;
    while((c = getchar()) != '\n')
    {
        if(c == 'P')   P++;
        if(c == 'A')   PA = (PA + P) % LIM;
        if(c == 'T')   PAT = (PAT + PA) % LIM;
    }
    printf("%d", PAT);
    return 0;
}

你可能感兴趣的:(PAT 1040 有几个PAT (25 分))