Pat 甲级 训练真题集 1093!

1093. Count PAT’s (25)

​ 时间限制

​ 120 ms

​ 内存限制

​ 65536 kB

​ 代码长度限制

​ 16000 B

​ 判题程序

​ Standard

​ 作者

​ CAO, Peng

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

个数为np*nt每遇到一个A,和相加得出结果

代码如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	cin>>s;
	long long cnt=0;
	long long countp=0,countt=0;
	for(int i=0;i<s.size();++i){
		if(s[i]=='T') ++countt;
	}
	for(int i=0;i<s.size();++i){
		if(s[i]=='P') ++countp;
		if(s[i]=='T') --countt;
		if(s[i]=='A'){
			cnt=(cnt+countp*countt%1000000007)%1000000007;
		}
	}
	printf("%lld",cnt);
	return 0;
}

原文地址

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦