Gym 100712B Rock-Paper-Scissors (前缀和维护)

/**********


B. Rock-Paper-Scissors




Rock-Paper-Scissors is a two-player game, where each player chooses one of Rock, Paper, or Scissors. Here
are the three cases in which a player gets one point:
­ Choosing Rock wins over a player choosing scissors.
­ Choosing Scissors wins over a player choosing Paper.
­ Choosing Paper wins over a player choosing Rock.
In all other cases, the player doesn’t get any points.
Bahosain and his friend Bayashout played N​ro​unds of this game. Unlike Bayashout, Bahosain was too lazy to
decide what to play next for each round, so before starting to play, he chose three integers X Y Z​s​uch that
X+Y+Z = N​a​nd X, Y, Z ≥ 0,​and then played Rock for the first X ​rounds, Paper for the next Y​ rounds, and
Scissors for the last Z ​rounds.
Bayashout got more points in the N r​ounds and won. Given the moves played by Bayashout in each round,
Bahosain wants to know the number of ways in which he could have chosen X,​Y​a​nd Z s​uch that he wins in
the N r​ounds.
The winner of the N r​ounds is the player that gets more total points in the N​r​ounds.




Input
The first line of input contains T (1 ≤ T ≤ 64),​where T i​s the number of test cases.
The first line of each test case contains an integer N (1 ≤ N ≤ 1000)​that represents the number of rounds.
The next line contains a string of N​uppercase letters, the first letter represents the choice of Bayashout for
the first round, the second letter represents his choice for the second round, and so on.
Each letter in the string is one of the following: R (R​ock), P​(Paper), or S​(Scissors).




Output
For each test case, print a single line with the number of ways in which Bahosain could have won.




Sample Input:Sample Output
4
3
RPS
1
R
5
PPRSR
5
RPSPR


Sample Output:
3
1
1
5


**********/


题意很简单,就是给出A出石头剪刀布的顺序,同时B是先出X个布,Y个石头,Z个剪刀,每局胜者得一分,求进行N轮游戏后B的分数比A多的种数。

首先维护6个前缀和,比如Rwin[i]代表前i个都出石头时B赢的总局数,以此类推。N最大是1000,所以可以双重循环暴力求解。



#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1050;
int Rwin[MAXN],Pwin[MAXN],Swin[MAXN],Rlose[MAXN],Plose[MAXN],Slose[MAXN],len;
char str[MAXN];

int main()
{
    //freopen("in.txt","r",stdin);
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        memset(Rwin,0,sizeof(Rwin));
        memset(Pwin,0,sizeof(Pwin));
        memset(Swin,0,sizeof(Swin));
        memset(Rlose,0,sizeof(Rlose));
        memset(Plose,0,sizeof(Plose));
        memset(Slose,0,sizeof(Slose));
        scanf("%d%s",&len,str);
        for(int i=0;i<len;i++)
        {
            if(i==0)
            {
                if(str[0]=='R')
                {
                    Pwin[1]=1;
                    Slose[1]=1;
                }
                if(str[0]=='P')
                {
                    Swin[1]=1;
                    Rlose[1]=1;
                }
                if(str[0]=='S')
                {
                    Rwin[1]=1;
                    Plose[1]=1;
                }
                continue;
            }
            Pwin[i+1]=Pwin[i];
            Swin[i+1]=Swin[i];
            Rwin[i+1]=Rwin[i];
            Plose[i+1]=Plose[i];
            Slose[i+1]=Slose[i];
            Rlose[i+1]=Rlose[i];
            if(str[i]=='R')
            {
                Pwin[i+1]=Pwin[i]+1;
                Slose[i+1]=Slose[i]+1;
            }
            if(str[i]=='P')
            {
                Swin[i+1]=Swin[i]+1;
                Rlose[i+1]=Rlose[i]+1;
            }
            if(str[i]=='S')
            {
                Rwin[i+1]=Rwin[i]+1;
                Plose[i+1]=Plose[i]+1;
            }
        }
        int ans=0;
        for(int i=0;i<=len;i++)
        {
            for(int j=i;j<=len;j++)
            {
                if(Rwin[i]+Pwin[j]-Pwin[i]+Swin[len]-Swin[j]>Rlose[i]+Plose[j]-Plose[i]+Slose[len]-Slose[j])
                {
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值