1. 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 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

/*
题意;A B猜拳,猜N次
但是A很懒,不想去想怎么猜拳就随机的连续出x次石头 y次布 z次剪刀
现在给出B的猜拳的出拳的顺序,
然后A以石头R 布P 剪刀S 的顺序出,在出了布之后就不在出石头,出了剪刀后就不再出石头和布
问A有多少种可能赢B。 这题一开始不怎么会做,做到限时赛最后还有30分钟的时候又回过头来看这个题目
不就是一个简单的前缀和么,一开始都没想到。 AC代码:
*/
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define MX 1000 + 50
using namespace std;
int r[MX],p[MX],s[MX],len;
char str[MX]; int main() {
int T;
scanf("%d",&T);
while(T--) {
memset(r,0,sizeof(r));
memset(p,0,sizeof(p));
memset(s,0,sizeof(s));
memset(str,0,sizeof(str));
scanf("%d%s",&len,str+1);
for(int i=1; i<=len; i++) {
if(str[i]=='R') {    //这里用数组rps记录全部出石头布剪刀的得分 【dp-前缀和】
p[i]=p[i-1]+1; //如果B出的石头 则A的布得分 +1
r[i]=r[i-1]; //A的石头平局,不变
s[i]=s[i-1]-1; //A的剪刀输扣分 -1
} //下面一样
if(str[i]=='S') {
r[i]=r[i-1]+1;
s[i]=s[i-1];
p[i]=p[i-1]-1;
}
if(str[i]=='P') {
s[i]=s[i-1]+1;
p[i]=p[i-1];
r[i]=r[i-1]-1;
}
}
int ans=0;
for(int i=0; i<=len; i++) { //i-1 为连续出石头的次数 r[i]-r[0]为A出石头的得分
for(int j=i; j<=len; j++) { //j-i为连续出布的次数 p[j]-p[i]为A出布的得分
if(r[i]-r[0]+p[j]-p[i]+s[len]-s[j]>0) { //len-j 为连续出布的次数 s[len]-p[j]为A出剪刀的得分
ans++; //计算和 如果比0大就算赢。
}
}
}
printf("%d\n",ans);
}
return 0;
}

  

ACM: 限时训练题解-Rock-Paper-Scissors-前缀和的更多相关文章

  1. ACM: 限时训练题解-Runtime Error-二分查找

    Runtime Error   Bahosain was trying to solve this simple problem, but he got a Runtime Error on one ...

  2. ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举

    Heavy Coins   Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always ...

  3. ACM: 限时训练题解- Travelling Salesman-最小生成树

    Travelling Salesman   After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...

  4. ACM: 限时训练题解-Epic Professor-水题

    Epic Professor   Dr. Bahosain works as a professor of Computer Science at HU (Hadramout    Universit ...

  5. ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】

    Street Lamps   Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...

  6. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...

  7. 【题解】CF1426E Rock, Paper, Scissors

    题目戳我 \(\text{Solution:}\) 考虑第二问,赢的局数最小,即输和平的局数最多. 考虑网络流,\(1,2,3\)表示\(Alice\)选择的三种可能性,\(4,5,6\)同理. 它们 ...

  8. 题解 CF1426E - Rock, Paper, Scissors

    一眼题. 第一问很简单吧,就是每个 \(\tt Alice\) 能赢的都尽量让他赢. 第二问很简单吧,就是让 \(\tt Alice\) 输的或平局的尽量多,于是跑个网络最大流.\(1 - 3\) 的 ...

  9. SDUT 3568 Rock Paper Scissors 状压统计

    就是改成把一个字符串改成三进制状压,然后分成前5位,后5位统计, 然后直接统计 f[i][j][k]代表,后5局状压为k的,前5局比和j状态比输了5局的有多少个人 复杂度是O(T*30000*25*m ...

随机推荐

  1. 理解Java中的final和static关键字

    回顾这两个关键字前,先考虑一个问题: Static变量存储在JVM中的位置,或者说static变量是如何被加载的? JVM会把类的静态方法和静态变量在类加载的过程中读入方法区(Method Area) ...

  2. Instagram的持续部署技术

    Instagram最近发表了一篇关于他们的持续部署(CD)管道的文章,持续部署管道可以让他们更快的将代码推送到生产环境,并且轻松地识别糟糕的提交和始终保持发布可用.在一段时间内以迭代的方式放在一起,其 ...

  3. Axure 全局辅助线(转)

    普通辅助线作用于当前页 全局作用于所有页面 , 包括新建页面 创建普通辅助线直接拉出来 创建全局辅助线 , 在拉出来的时候按住 Ctrl 默认情况下 , 颜色不同 辅助线可以多选 , 用拖选 或 按 ...

  4. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  5. ☆ ☆ VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程 (转)

    参考  http://diybbs.zol.com.cn/1/34037_699.html 然后对安装的Mac系统进行升级到最新版本. 安装mac系统之后,再安装VMTOOLS darwin. 方法可 ...

  6. hdu 4568 Hunter(spfa预处理 + 状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:首先spfa预处理出每对宝藏之间的最短距离以及宝藏到边界的最短距离,然后dp[state] ...

  7. ios内购

    1.添加框架,storeKit.framework 需要真机调试 /* 内购五步: 1.请求可销售商品的列表 2.展示课销售的商品 3.点击购买 4.开具小票 5.创建交易对象并添加到交易队列 6.创 ...

  8. currentStyle

    用js的style属性可以获得html标签的样式,但是不能获取非行间样式. 解决方法: 在IE下可以用currentStyle; 在FF下用getComputedStyle; 然而,为了让其兼容,解决 ...

  9. winform基础窗体设置及基础控件

    WinForm - 也叫做C/S  客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...

  10. 删除表数据drop、truncate和delete的用法

    说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...