rock-paper-scissors
维护三个前缀和,然后注意顺序,最后做差来确定可行的答案,因为答案比较小,可以考虑这种暴力做法,像这种方案数可以++的题真的不多,如果想不出来特别优秀的想法,不妨简单化思维
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define ls rt<<1 13 #define rs rt<<1|1 14 #define lson ls,nl,mid,l,r 15 #define rson rs,mid+1,nr,l,r 16 #define N 100010 17 #define For(i,a,b) for(int i=a;i<=b;i++) 18 #define p(a) putchar(a) 19 #define g() getchar() 20 21 using namespace std; 22 int R[1010],S[1010],P[1010]; 23 int n,m; 24 int T; 25 char a[1010]; 26 int cnt; 27 void in(int &x){ 28 int y=1; 29 char c=g();x=0; 30 while(c<'0'||c>'9'){ 31 if(c=='-')y=-1; 32 c=g(); 33 } 34 while(c<='9'&&c>='0'){ 35 x=(x<<1)+(x<<3)+c-'0';c=g(); 36 } 37 x*=y; 38 } 39 void o(int x){ 40 if(x<0){ 41 p('-'); 42 x=-x; 43 } 44 if(x>9)o(x/10); 45 p(x%10+'0'); 46 } 47 int main(){ 48 in(T); 49 while(T--){ 50 cnt=0; 51 in(n); 52 cin>>(a+1); 53 For(i,1,n){ 54 if(a[i]=='R'){ 55 R[i]=R[i-1]; 56 S[i]=S[i-1]-1; 57 P[i]=P[i-1]+1; 58 } 59 if(a[i]=='S'){ 60 R[i]=R[i-1]+1; 61 S[i]=S[i-1]; 62 P[i]=P[i-1]-1; 63 } 64 if(a[i]=='P'){ 65 R[i]=R[i-1]-1; 66 S[i]=S[i-1]+1; 67 P[i]=P[i-1]; 68 } 69 } 70 For(i,0,n) 71 For(j,0,n) 72 if(i+j<=n&&R[i]+P[i+j]-P[i]+S[n]-S[j+i]>0) 73 cnt++; 74 o(cnt);p(' '); 75 } 76 return 0; 77 }