美文网首页
PAT 1040 有几个PAT (25 分)

PAT 1040 有几个PAT (25 分)

作者: 昭明ZMing | 来源:发表于2019-06-06 14:10 被阅读0次
    #include <iostream>
    #include <string>
    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 <stdio.h>
    #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 分)

          本文链接:https://www.haomeiwen.com/subject/hvzyxctx.html