pat1040有多少个pat。如何解决超时问题

该博客探讨了PAT1040题目中遇到的超时问题,提出通过使用存储空间来避免运行时间过长的策略。在输入过程中,使用vector存储每个字符左侧已出现的'p'和't'的数量。通过这种方式,可以快速查找'a'的位置并计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此题关键是用存储空间去换取时间。否则老是超时。

在输入时就用一个vector《char》不断记录每一位时左边已经有的p,t。最后查找到a看对应位置左边的p个数,t个数,在求结果。

代码如下

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
typedef vector<char>::iterator ite;
int main(){
	vector<char> vec;
	vector<int> pleft, tleft;
	char ch;
	long long  num = 0;
	int p=0, t=0;//随手初始化!!!!!

	while (ch = getchar()){
		if (ch == '\n')
			break;
		if (ch == 'P'){
			p++;
			
		}
		if (ch == 'T'){
			t++;			
		}
		pleft.push_back(p);
		tleft.push_back(t);//pleft tleft vec 同时增长
		vec.push_back(ch);
	}
	ite A = vec.begin();
	while (A != vec.end()){
		A = find(A, vec.end(), 'A');
		if (A != vec.end()){
			num += pleft[A - vec.begin()] * (t - tleft[A - vec.begin()]);
			A++;
		}
	}
	
	//这个方法还是要遍历好多次没办法用。不如价格变量,在输入时就记下来有多少pt、
	//ite A = vec.begin();//少用+n来算。容易越界。
	//while (A != vec.end()){
	//	A = find(A, vec.end(), 'A');
	//	p = count(vec.begin(), A, 'P');
	//	t = count(A , vec.end(), 'T');
	//	num += p*t;
	//	if (A != vec.end())
	//		A++;
	//}


	//老方法为先找到p再找a再找t,这样遍历次数比较多超时。新方法网上找到,先找到a左边p个数乘以右边t个数
//	ite P = vec.begin(), A = vec.begin(), T = vec.begin();
//	while (P!=vec.end()){//reduce to use。erase because it can change the iterator begin.
//		P = find(P, vec.end(), 'P');
//		if (P != vec.end())
//			P++;
//
//		A = P;
//		while (A != vec.end()){
//			A = find(A , vec.end(), 'A');
//			num += count(A, vec.end(), 'T');
//			if (A!=vec.end())
//				A++;//不要随意自加。万一已经到了end再自加就会溢出。
//		}
//	}
	cout << num%1000000007;
	return 0;
}

//方法错误,不应该是从首尾同时删除。patpapatpatpatp这样就不行了
//#include<vector>
//#include<iterator>
//#include<iostream>
//#include<algorithm>
//
//using namespace std;
//
//int main(){
//	vector<char> vec;
//	int num = 0;
//	char ch = 'P';
//	while (ch=getchar()){//cin不读取回车,但是getchar读取。
//		if (ch=='\n')//又把等于和赋值搞混了
//			break;
//		vec.push_back(ch);
//	}
//	while (!vec.empty()){
//		if (flag % 2 = 1){
//			vector<char>::iterator P = find(vec.begin(), vec.end(), 'P');
//			vec.erase(vec.begin(), P - 1);
//			if (P == vec.end())
//				break;
//		}
//		if(flag%2=0){
//			vector<char>::reverse_iterator T =
//				find(vec.rbegin(), vec.rend(), 'T');
//			if (T == vec.rend())
//				break;
//			vec.erase(T.base() - 1, vec.end());
//		}
//		if (P<T.base())
//		num += count(P, T.base(), 'A');
//		
//		
//	}
//	cout << num % 1000000007;//取余数不是除数
//}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值