Codeforce - 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 rounds 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 such that X+Y+Z = N and 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 rounds 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 and Z such that he wins in the N rounds.

The winner of the N rounds is the player that gets more total points in the    N rounds.

Input

The first line of input contains T (1 ≤ T ≤   64), where T is 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 (Rock), 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

3

1

RPS

1

1

5

R

 

5

 

PPRSR

 

5

 

RPSPR

 

 

这道题刚才我居然用递归去做,做了好久写了一个递归出来,结果是递归只能过样例,真是惨不忍睹的结局,哎,看看别人的方法,前缀和,多好,用三个数组表示前i局出不同手势的得分

最后来统计,不过统计的时候注意区间的划分,在最后统计的时候最好自己画个图,这样就很清晰了,画个区间图,还有要注意的是这题的出拳顺序是有规定的,必须是RPS,这样的顺序

就是说R在P前面,P在S前面

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;

#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int MX = 1111;
int r[MX], p[MX], s[MX];

void ini() {
    memset(r, 0, sizeof(r));
    memset(p, 0, sizeof(p));
    memset(s, 0, sizeof(s));
}

//使用前缀和处理问题
int main() {
    //freopen("input.txt", "r", stdin);
    int n;
    char str[MX];
    int cas;
    while (scanf("%d", &cas) != EOF) {
        while (cas--) {
            ini();
            scanf("%d %s", &n, str + 1);
            for (int i = 1; i <= n; i++) {
                if (str[i] == 'R') {
                    r[i] = r[i - 1];
                    p[i] = p[i - 1] + 1;
                    s[i] = s[i - 1] - 1;
                } else if (str[i] == 'P') {
                    r[i] = r[i - 1] - 1;
                    p[i] = p[i - 1];
                    s[i] = s[i - 1] + 1;
                } else {
                    r[i] = r[i - 1] + 1;
                    p[i] = p[i - 1] - 1;
                    s[i] = s[i - 1];
                }
            }
            int ans = 0;
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= n - i; j++) {
                    if (r[i] + p[i + j] - p[i] + s[n] - s[i + j] > 0) {
                        ans++;
                    }
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}
posted @ 2016-07-27 10:57  苍鼠  阅读(266)  评论(0编辑  收藏  举报