码迷,mamicode.com
首页 > 其他好文 > 详细

PAT Advanced 1093 Count PAT's (25分)

时间:2020-01-20 09:57:49      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:spec   line   amp   first   system   may   nbsp   The   etl   

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分)

标签:spec   line   amp   first   system   may   nbsp   The   etl   

原文地址:https://www.cnblogs.com/littlepage/p/12216152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!