1040 有几个PAT (25分)

25分题中不是很难的一道,没错我就是挑软柿子捏,今天一定要把basic搞完,阿弥陀佛别拖拉了。。。



第一次提交后三个测试点运行超时(循环过多吧)。。。。一开始是每遇到一个A就从前往后循环看这个A前面有多少P,有多少T。

第二次提交后后两个测试点答案错误,但是不超时了!微笑 -

由于结果可能比较大,只输出对 1000000007取余数的结果。(题目中的说明)

不要忽略这一句,这说明了个数有可能很大,所以将sum改为longlong型后面两个测试点就过了!!

呵呵。。。。



算法思想就是记录每一个A前后的P、T个数,然后乘一下再加一下~

#include<iostream>
#include<string>

using namespace std;

int main() {
	string a;
	int i, j, k;
	int p[100001] = { 0 };
	int t[100001] = { 0 };
	long long int sum = 0;
	int ptemp = 0, ttemp = 0;
	getline(cin, a);
	j = 0;
	//从前往后统计每一个A前P的个数
	for (i = 0; i < a.length(); i++) {
		if (a[i] == ‘P‘) {
			ptemp++;
		}
		if (a[i] == ‘A‘) {
			p[j++] = ptemp;
		}
	}
	k = 0;
	//从后往前统计每一个人A后T的个数
	for (i = a.length() - 1; i >= 0; i--) {
		if (a[i] == ‘T‘) {
			ttemp++;
		}
		if (a[i] == ‘A‘) {
			t[k++] = ttemp;
		}
	}
	//p--123
	//t--321
	for (i = 0; i < j; i++) {
		sum += p[i] * t[j - i - 1];
	}

	cout << sum % 1000000007 << endl;
	return 0;
}

原文地址:https://www.cnblogs.com/tanghm/p/12658617.html

时间: 2024-07-30 15:29:42

1040 有几个PAT (25分)的相关文章

PTA乙级 (*1040 有几个PAT (25分))

1040 有几个PAT (25分) https://pintia.cn/problem-sets/994805260223102976/problems/994805282389999616 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <cstring> typedef lo

PAT Advanced 1153 Decode Registration Card of PAT (25 分)

A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic; the 2nd - 4th digits are the test site number, ranged from 101 to 999; the 5th - 10th digits

浙大pat1040 Longest Symmetric String(25 分)

1040 Longest Symmetric String(25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. Inp

PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

PTA 10-排序5 PAT Judge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Spe

1025 PAT Ranking (25 分)

1025 PAT Ranking (25 分) Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately

PAT Advanced 1093 Count PAT&#39;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 numb

PTA乙级 (*1095 解码PAT准考证 (25分))

1095 解码PAT准考证 (25分) https://pintia.cn/problem-sets/994805260223102976/problems/1071786104348536832 题目大意:给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和个人编号信息,并有三种查询方式查询一:给出考试等级,找出该等级的考生,按照成绩降序,准考证升序排序查询二:给出考场号,统计该考场的考生数量和总得分查询三:给出考试日期,查询改日期下所有考场的考试人数,按照人数降序,考

1025 PAT Ranking (25分)

1025 PAT Ranking (25分) 1. 题目 2. 思路 设置结构体, 先对每一个local排序,再整合后排序 3. 注意点 整体排序时注意如果分数相同的情况下还要按照编号排序 4. 代码 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; struct stu{ int location_number; char