1093 Count PAT‘s (25 分)

本文介绍了一种算法,用于计算给定字符串中特定子串“PAT”的出现次数。该算法通过遍历字符串并利用预计算的P字符和T字符数量来高效地找出所有可能的组合。

在这里插入图片描述
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 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

#include<iostream>
using namespace std;
int main(){
	string s;
	cin >> s;
	int len = s.length();
	int leftNumP[100005] = { 0 };
	for (int i = 0; i < len; i++) {
		if (i > 0) leftNumP[i] = leftNumP[i - 1];
		if (s[i] == 'P') leftNumP[i]++;
	}
	int ans = 0, rightNumT = 0;
	for (int j = len - 1; j >= 0; j--) {
		if (s[j] == 'T') rightNumT++;
		else if (s[j] == 'A') ans = (ans + leftNumP[j] * rightNumT) % 1000000007;
	}
	cout << ans;
	return 0;
}
03-25
当前提供的引用内容并未涉及 PAT 1003 的具体描述或解决方案。然而,可以基于 PAT 题目的常见模式以及算法竞赛的知识体系来推测其可能的内容。 通常情况下,PAT(Programming Ability Test)中的题目会围绕常见的数据结构与算法展开,例如字符串处理、动态规划、图论、贪心算法等。对于 PAT 1003,虽然具体的题目尚未提供,但可以根据 PAT 考试的特点推断出一些通用的信息: ### 可能的主题范围 如果 PAT 1003 是一道典型的编程题,则它可能会涉及到以下主题之一: - **字符串操作**:如子串匹配、正则表达式应用等。 - **数组/列表操作**:查找最大值、最小值或者特定条件下的元素组合。 - **基本算法设计**:如排序、二查找或其他基础算法的应用。 以下是针对假设场景下的一般性讨论和代码实现示例。 --- #### 假设情景一:字符串处理类问题 假如 PAT 1003 涉及到字符串的操作,比如统计字符频率并按某种规则排序输出,那么可以用如下方法解决: ```python from collections import Counter def process_string(s): count = Counter(s) # 统计每个字符出现次数 result = sorted(count.items(), key=lambda x: (-x[1], ord(x[0]))) # 排序逻辑 return ''.join([char * freq for char, freq in result]) input_str = input().strip() output_str = process_string(input_str) print(output_str) ``` 上述代码实现了对输入字符串中各字符按照频次降序排列的功能[^4]。若有相同频次,则依据 ASCII 编码顺序升序排列。 --- #### 假设情景二:简单数值计算型问题 另一种可能性是该题属于简单的数值运算范畴,例如求解一组数列的平均值及其偏差情况。 ```python import math def calculate_statistics(numbers): mean_value = sum(numbers) / len(numbers) variance = sum((num - mean_value)**2 for num in numbers) / len(numbers) std_deviation = math.sqrt(variance) return round(mean_value, 2), round(std_deviation, 2) raw_input = list(map(float, input().split())) mean, deviation = calculate_statistics(raw_input) print(f"{mean} {deviation}") ``` 此段程序能够接收一系列浮点数作为输入,并返回它们的均值与标准差结果[^5]。 --- 尽管目前无法确切得知 PAT 1003 的具体内容,但从以往经验来看,大多数此类考试都会集中考察考生的基础编码能力与逻辑思维水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值