【PAT】1093 Count PAT's 解题报告

本文介绍了一种算法,用于计算给定字符串中特定子串'PAT'出现的次数。通过预先计算左侧P的数量和右侧T的数量,再结合中间A的位置,有效地解决了这一问题。最终结果对1000000007取模,适用于处理大量数据。

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 10​5​​ 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

题解

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100010; 
const int mod = 1000000007;
int leftNumP[maxn] = {0};//每一位左边含p的个数 
int main()
{
	char str[maxn];
	while(scanf("%s", str) != EOF){
		int len = strlen(str);
		int i;
		for(i = 0; i < len; i++){
			if(i > 0){
				leftNumP[i] = leftNumP[i - 1];
			}
			if(str[i] == 'P'){
				leftNumP[i]++;
			}
		}
		int ans = 0, rightNumT = 0;
		for(i = len - 1; i >= 0; i--){
			if(str[i] == 'T'){
				rightNumT++;
			}
			else if(str[i] == 'A'){
				ans = (ans + leftNumP[i] * rightNumT) % mod;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

 

源码下载地址: https://pan.quark.cn/s/a4b39357ea24 谷歌公司设计了一款无费用且具备开源特性的网络浏览器,名为Chrome,因其卓越的速度、稳定性和安全性而广受赞誉。该浏览器运用了前沿的Web渲染引擎Blink以及JavaScript引擎V8,旨在保障网页载入与脚本运行的卓越效能。为应对无网络环境下的Chrome安装需求,特别准备了离线安装包。此压缩文件内含32位与64位两种规格的Chrome浏览器离线安装方案,具体文件名分别为"chromedev_x64-v68.0.3423.2.exe"与"chromedev_x86-v68.0.3423.2.exe"。在文件命名中,"x64"标识64位版本,适用于64位操作系统平台,而"x86"则对应32位版本,适配32位操作系统。文件名中的"v68.0.3423.2"代表Chrome的一个特定版本号,各版本可能涵盖安全补丁、性能改进或新增功能。与32位Chrome相比,64位版本具备如下长处:能够处理更多内存容量,从而提升多任务作业能力;针对现代硬件的优化使其运行更为迅猛;64位版本更具备高级别的安全防护,能更周全地抵御恶意软件的侵袭。尽管如此,32位版本对于仍在使用32位操作系统的用户,或是在系统资源需求不高的场景下,依然适用。在部署Chrome浏览器时,用户需依据其个人计算机的操作系统平台,挑选匹配的版本进行安装。通过双击相应的.exe文件,安装流程将自动启动,一般包含接受使用许可、确定安装路径及构建桌面快捷方式等环节。若在安装阶段遭遇难题,可参照提示信息或联系技术支援获取协助,同时该压缩文件发布者亦表明欢迎用户以留言形式反映问题。Chrome浏览器的主要特质涵盖:直观的用户界面设计...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值