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 Nrounds 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 Zsuch that
X+Y+Z = Nand 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,Yand 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 Nrounds.
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 Nuppercase 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
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,所以可以双重循环暴力求解。
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 Nrounds 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 Zsuch that
X+Y+Z = Nand 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,Yand 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 Nrounds.
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 Nuppercase 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
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; }
相关文章推荐
- linux下的netstat小结
- 总结linux无线命令wpa
- CentOS7下安装PostgresSQL9.4.4
- 9.19 搭服务器折腾centos
- linux挂载mount参数优化
- Linux进程间通信——使用信号
- Linux进程间通信——信号集函数
- Linux进程间通信——使用匿名管道
- Linux进程间通信——使用命名管道
- Linux进程间通信——使用信号量
- 网站备案问题指南
- 网站备案问题指南
- Linux进程间通信——共享内存
- Linux进程间通信 ————消息队列
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- 网站制作流程
- Nginx+Keepalived主备切换(包含nginx服务停止)
- linux程序设计——IPC状态命令(第十四章)
- Tomcat虚拟目录的配置
- shell脚本-批量添加或删除用户