Codeforce - Rock-Paper-Scissors

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值