列表

详情


NC24055. [USACO 2017 Jan S]Hoof, Paper, Scissors

描述

You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game they call "Hoof, Paper, Scissors".

The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each-other. They both count to three and then each simultaneously makes a gesture that represents either a hoof, a piece of paper, or a pair of scissors. Hoof beats scissors (since a hoof can smash a pair of scissors), scissors beats paper (since scissors can cut paper), and paper beats hoof (since the hoof can get a papercut). For example, if the first cow makes a "hoof" gesture and the second a "paper" gesture, then the second cow wins. Of course, it is also possible to tie, if both cows make the same gesture.

Farmer John wants to play against his prize cow, Bessie, at N games of "Hoof, Paper, Scissors" (). Bessie, being an expert at the game, can predict each of FJ's gestures before he makes it. Unfortunately, Bessie, being a cow, is also very lazy. As a result, she tends to play the same gesture multiple times in a row. In fact, she is only willing to switch gestures at most once over the entire set of games. For example, she might play "hoof" for the first x games, then switch to "paper" for the remaining N−x games.

输入描述

The first line of the input file contains N.
The remaining N lines contains FJ's gestures, each either H, P, or S.

输出描述

Print the maximum number of games Bessie can win, given that she can only change gestures at most once.

示例1

输入:

5
P
P
H
P
S

输出:

4

原站题解

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

Python3(3.9) 解法, 执行用时: 112ms, 内存消耗: 11864K, 提交时间: 2020-12-13 22:47:45

def solve(S:str)->int:
    p=h=s=0
    pp=hh=ss=0
    for x in S:
        if x=="P":
            pp+=1
        elif x=="H":
            hh+=1
        else:
            ss+=1
    ans=max(pp,hh,ss)
    for x in S:
        if x=="P":
            p+=1
            pp-=1
        elif x=="H":
            h+=1
            hh-=1
        else:
            s+=1
            ss-=1
        ans=max(ans,max(p,h,s)+max(pp,hh,ss))
    return ans

import sys
lines=sys.stdin.readlines()
s=list(map(str.strip,lines[1:]))
print(solve(s))

C++11(clang++ 3.9) 解法, 执行用时: 12ms, 内存消耗: 2296K, 提交时间: 2020-04-01 16:05:22

#include<bits/stdc++.h>
using namespace std;
int n,r,a[1<<20][5];
char s[3];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	scanf("%s",s),a[i][s[0]=='P'?0:s[0]=='H'?1:2]++;
	for(int i=1;i<=n;i++)
	for(int j=0;j<3;j++)
	a[i][j]+=a[i-1][j];
	for(int i=1;i<=n;i++)
	r=max(r,max(a[i][0],max(a[i][1],a[i][2]))+max(a[n][0]-a[i][0],max(a[n][1]-a[i][1],a[n][2]-a[i][2])));
	printf("%d\n",r);
}

上一题