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;
}

Codeforce - Rock-Paper-Scissors的更多相关文章

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

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

  2. SDUT 3568 Rock Paper Scissors 状压统计

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

  3. FFT(Rock Paper Scissors Gym - 101667H)

    题目链接:https://vjudge.net/problem/Gym-101667H 题目大意:首先给你两个字符串,R代表石头,P代表布,S代表剪刀,第一个字符串代表第一个人每一次出的类型,第二个字 ...

  4. Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数

    Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...

  5. Gym101667 H. Rock Paper Scissors

    将第二个字符串改成能赢对方时对方的字符并倒序后,字符串匹配就是卷积的过程. 那么就枚举字符做三次卷积即可. #include <bits/stdc++.h> struct Complex ...

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

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

  7. 题解 CF1426E - Rock, Paper, Scissors

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

  8. HDOJ(HDU) 2164 Rock, Paper, or Scissors?

    Problem Description Rock, Paper, Scissors is a two player game, where each player simultaneously cho ...

  9. HDU 2164 Rock, Paper, or Scissors?

    http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...

  10. 1090-Rock, Paper, Scissors

    描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...

随机推荐

  1. DOM - EventListener 句柄操作

          <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  2. WMI测试

    https://social.msdn.microsoft.com/Forums/windowshardware/zh-CN/c5af7959-95d3-4e1b-ab40-96a2a31c2af2/ ...

  3. Android ANR分析(1)

    转自:http://blog.csdn.net/itachi85/article/details/6918761 一:什么是ANR ANR:Application Not Responding,即应用 ...

  4. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  5. 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序

    1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...

  6. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  7. NBU bplabel命令擦除磁帶數據

    bplabel Linux系統,該命令位於NBU server的如下目錄:/usr/openv/netbackup/bin/admincmd bplabel – write NetBackup lab ...

  8. long和int的区别

    转自:http://blog.sina.com.cn/s/blog_6f62c9510101svjz.html 突然间就想到了long和int到底什么区别(发现有很多问题都是突然间想到的),然后百度. ...

  9. 在Salesforce中创建Web Service供外部系统调用

    在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,接下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用As ...

  10. How to use Ajax on Visualforce page on Salesforce platform

    Just use Ajax pattern to call object data from server on visualforce page. Following is the Asynchro ...