列表

详情


NC214629. Scissors-Paper

描述

Both Xiao Wang and Xiao Li are students at Hunan University. They need to play a game for fun. 
The name of this game is "Rock-Paper-Scissors" and there will be a total of n rounds. The winner gets 1 point, the loser gets -1 point, and the tie gets 0 points. The outcome of each turn is determined by the rules of Rock-Paper-Scissors. At this time an old man came over and told them: "Young people should speak martial arts". He said this game should satisfy the following two rules:
  1. each player plays one of the two gestures, Scissors and Paper.
  2. After each turn, (the number of times the player has played Scissors) ≦ (the number of times the player has played Paper).
Xiao Wang have learned Hun Yuan Gong Fa, so he can know in advance what Xiao Li will play in each round. Plan Xiao Wang's gesture in each turn to maximize Xiao Wang's score.
(For those who are not familiar with Rock-paper-scissors: If one player plays Paper and the other plays Scissors, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)

输入描述

The first line contains one integer T, the number of test cases.
For each test case, there are two lines. 
The first line contains an integer n -the length of the string. 
The second line contains a string consists of only 'S' and 'P'. 
If the i-th character is 'S', Xiao Li will play Scissors in the i-th turn. Similarly, if the i-th character is 'P', Xiao Li will play Paper in the i-th turn.(T ≤ 100, ∑n≤1e6)

输出描述

For each case, your program will output only a line contains a single integer — the Xiao Wang's maximum possible score.

示例1

输入:

2
3
PSP
10
PPSSPPPSPP

输出:

0
2

说明:

For the first case, playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C(clang11) 解法, 执行用时: 4ms, 内存消耗: 484K, 提交时间: 2020-12-27 13:42:59

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int a,i,s=0,p=0;
		char x[1000000];
		scanf("%d",&a);
		scanf("%s",x);
		for(i=0;i<a;i++)
		{
			if(x[i]=='S') s++;
			if(x[i]=='P') p++;
		}
		int k=a/2;
		printf("%d\n",k-s);
	}
	return 0;
}

C++(clang++11) 解法, 执行用时: 26ms, 内存消耗: 720K, 提交时间: 2021-01-06 13:11:01

#include<iostream>
#include<string>
using namespace std;
int main(){
int t;
cin>>t;
int n;
string s;
while(t--){
cin>>n>>s;
int num=0;
for(int i=0;i<s.length();i++){
   if(s[i]=='S')
     num++;
}
    cout<<n/2-num<<endl;


}

}

Python3(3.9) 解法, 执行用时: 19ms, 内存消耗: 3356K, 提交时间: 2020-12-27 14:39:07

a=int(input())
for i in range(a):print((int(input())//2)-input().count('S'))

上一题