Pat(Advanced Level)Practice--1093(Count PAT's)

来源:互联网 发布:消防调试联动编程 编辑:程序博客网 时间:2024/06/15 15:06

Pat1093代码

题目描述:

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

AC代码:
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#define MAXN 100005#define MOD 1000000007using namespace std;int main(int argc,char *argv[]){char str[MAXN];scanf("%s",str);int numT=0;int numAT=0;int numPAT=0;for(int i=strlen(str)-1;i>=0;i--){if(str[i]=='T'){++numT;}else if(str[i]=='A'){//记录AT组合的个数,其中包括之前统计的AT个数,加上这个A与后面全部T组合的个数numAT=(numAT+numT)%MOD;}else{numPAT=(numPAT+numAT)%MOD;}}printf("%d\n",numPAT);return 0;}

0 0
原创粉丝点击