1093. Count PAT's (25)

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 105 characters containing only P, A, 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


这一题刚拿到手我是没思路的,如果定义这道类型的话,我觉得应该属于打表降复杂度的题,有一定的子结构,后边的结果可以用前面计算的结果得到,这种技巧在处理时间复杂度的方面经常出现,比如停车数量那道题,可以用1和-1来累计当前时间的车辆。


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int maxn = 100010;
int main(){
     char ch[maxn];
     scanf("%s",ch);
     long long p = 0,pa = 0,pat = 0;
     for(int i = 0;i<strlen(ch);i++){
        if(ch[i]=='P')p++;
        else if(ch[i]=='A')pa+=p;
        else if(ch[i]=='T')pat+=pa;
     }
     printf("%d",pat%1000000007);
    return 0;

}

猜你喜欢

转载自blog.csdn.net/xiyuan1223/article/details/79244335