TLE求助
查看原帖
TLE求助
1194979
I_AM_joker楼主2024/3/10 20:20

主要是不懂怎么优化

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e7+5;
int q,op,l,r,score,a[maxn],all;
string str;//P=3,p=2,g=1,else=0
int main(){
	score=0;
	cin>>q;
	while (q--){
		cin>>str;//输入
		int size=str.size();
		for (int i = 0;i<size;i++){
			if (str[i]=='P') a[i]=3;
			else if (str[i]=='p') a[i]=2;
			else if (str[i]=='G') a[i]=1;
			else a[i]=0;
		}//统计
		cin>>op;
		while (op--){
			int l,r;
			cin>>l>>r;
			l--;r--;
			for (int i = l;i<=r;i++){
				score+=a[i];
			}
			cout<<score<<endl;
			score=0;
		}//计算+输初
		//cout<<"a[]:";
		/*for (int i = 0;i<size;i++){
			cout<<a[i]<<" ";
		}
		cout<<endl;*/
	}
	return 0;
}
2024/3/10 20:20
加载中...