首页 > 其他 > 详细

PAT Advanced 1093 Count PAT's (25分)

时间:2020-01-20 10:03:53      阅读:106      评论:0      收藏:0      [点我收藏+]
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分)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!