PAT (Advanced Level) Practice 1093 Count PAT‘s (25 分) 凌宸1642

PAT (Advanced Level) Practice 1093 Count PAT’s (25 分) 凌宸1642

题目描述:

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.

译:字符串 APPAPT 包含 2 个子串PAT。第一个由第二、第四和第六个字母组成,第二个由第三、第四和第六个字母组成。现在给你任意的字符串,你应该找出这个字符串中包含子串PAT 的个数。

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

译:每个输入文件包含一个测试。对于每个用例,在一行中给定一个只包含 P , A 或者T字母并且长度不超过 105 的字符串。


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.

译:对于每个测试用例,在一行中输出该字符串包含的子串 PAT的数量。因为结果可能是一个很大的整数,所以输出结果对 1000000007 取余的结果。


Sample Input (样例输入):

APPAPT

Sample Output (样例输出):

2

The Idea:

仔细理清 字母 P A T 之间的关系其实这一题很简单。

  • 思考子串 PPAPAT 之间的关系。
  • 从左至右依次遍历字符串,统计上述子串 PPAPAT 的数量,当遇到字母 P ,则 P 的数目自增 ; 当遇到 A 则此时有多少个 P , PA的数量增加多少个;当遇到 T 则此时有多少个 PA , PAT的数量增加多少个。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
ll p = 0 , pa = 0 , pat = 0 ;
string s ;
ll mod = 1000000007 ;

int main(){
	cin >> s ;
	for(int i = 0 ; i < s.size() ; i ++){
		if(s[i] == 'P') p = (++p) % mod ;
		else if(s[i] == 'A') pa = (pa + p) % mod ;
		else pat = (pat + pa) % mod ;
	}
	cout << pat << endl ;
	return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值