PAT Advanced 1093 Count PAT's (25分)

Posted littlepage

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT Advanced 1093 Count PAT's (25分)相关的知识,希望对你有一定的参考价值。

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 1 characters containing only PA, 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

计数方法,先计数T的个数,再遍历P增加,T减少,遇到一个A进行累加

#include <iostream>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    long long P=0,A=0,T=0,sum=0;
    for(int i=0;i<s.length();i++)
        if(s[i]==T) T++;
    for(int i=0;i<s.length();i++){
        if(s[i]==P) P++;
        if(s[i]==T) T--;
        if(s[i]==A) sum+=((P%1000000007*T%1000000007)%1000000007);
    }
    cout<<sum%1000000007;
    system("pause");
    return 0;
}

以上是关于PAT Advanced 1093 Count PAT's (25分)的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced 1093 Count PAT's (25分)

PAT Advanced level 1093

1093 Count PAT‘s

A1093 Count PAT's (25 分)

PAT1093: Count PAT's (25)

1093. Count PAT's (25)