首页 > 其他 > 详细

PAT甲级——A1093 Count PAT's【25】

时间:2019-08-13 23:58:41      阅读:146      评论: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

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     string input;
 8     getline(cin,input);
 9     int Pnum[input.size()]={0};//Pnum数组表示该字符左侧字符P的数量
10     for(int i=1;i<input.size();++i)
11     {//如果i-1位置是P字符,则Pnum[i]=Pnum[i-1]+1;否则Pnum[i]=Pnum[i-1]
12         Pnum[i]=Pnum[i-1];
13         if(input[i-1]==P)
14             ++Pnum[i];
15     }
16     int tnum=0,sumPAT=0;//tnum表示该字符右侧字符T的数量,sumPAT表示PAT字符串总数
17     for(int i=input.size()-1;i>=0;--i)
18         if(input[i]==T)
19             ++tnum;
20         else if(input[i]==A)
21             sumPAT=(sumPAT+Pnum[i]*tnum)%1000000007;
22     printf("%d\n",sumPAT);
23     return 0;
24 }

 

 

 

PAT甲级——A1093 Count PAT's【25】

原文:https://www.cnblogs.com/zzw1024/p/11349323.html

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