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;
}

猜你喜欢

转载自www.cnblogs.com/littlepage/p/12216152.html