Gym


题意:

A是固定出的, Rock for the first X​rounds, Paper for the next Y​rounds, andScissors for the last Z​rounds.,加起来为n,每个大于等于0.

给出B的出拳,问怎么赢:

思路:

直接枚举的话需要三段0~i,i~j,j~n三重有超时风险。这里预处理,把三种出法的n次情况全部存在数组里。

然后最后判断r[i]+p[j]-p[i]+s[n]-s[j] > 0即可。p[j]减去超重的p[i]得到二段,s[n]减去s[j]得到第三段


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
string str;
int r[maxn], s[maxn], p[maxn];
int n;

int main(){
//    freopen("in.txt","r",stdin);
	int T;
	cin>>T;
	while (T--){
		cin>>n;
		cin>>str;
		for (int i=1; i<=n; ++i){
			r[i] = r[i-1]+(str[i-1]=='R' ? 0 : (str[i-1]=='S' ? 1 : -1));
			s[i] = s[i-1]+(str[i-1]=='S' ? 0 : (str[i-1]=='P' ? 1 : -1));
			p[i] = p[i-1]+(str[i-1]=='P' ? 0 : (str[i-1]=='R' ? 1 : -1));
		}
		int ans = 0;
		for (int i=0; i<=n; ++i)
			for (int j=i; j<=n; ++j)
				if (r[i]+p[j]-p[i]+s[n]-s[j] > 0)
					ans++;
		cout<<ans<<endl;
	}
	return 0;
}


原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256610.html