您的位置:首页 > 运维架构

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

2015-07-19 23:57 387 查看
/**********

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: