原题下载
答案:
import java.io.*;
import java.util.*;
public class hps {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("hps.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("hps.out")));
int n = Integer.parseInt(br.readLine());
int[] l = new int[n];
int[] lReverse = new int[n];
for(int i = 0; i < n; i++) {
String s = br.readLine();
if(s.equals("P")) l[i] = 1;
else if(s.equals("S")) l[i] = 2;
lReverse[l.length-1-i] = l[i];
}
int[][] matchPrefix = getMatch(l);
int[][] matchSuffix = getMatch(lReverse);
int ret = 0;
for(int a = 0; a <= n; a++) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
ret = Math.max(ret, matchPrefix[i][a] + matchSuffix[j][n-a]);
}
}
}
pw.println(ret);
pw.close();
}
public static int[][] getMatch(int[] l) {
int[][] matches = new int[3][l.length+1];
for(int i = 0; i < l.length; i++) {
for(int j = 0; j < 3; j++) {
matches[j][i+1] = matches[j][i];
}
matches[l[i]][i+1]++;
}
return matches;
}
}
以上就是关于【USACO 2017 January Contest, Silver Problem 2. Hoof, Paper, Scissors】的解答,如需了解学校/赛事/课程动态,可至翰林教育官网获取更多信息。
往期文章阅读推荐:
USACO计算机奥赛如何认证成绩?2026赛季黄金铂金组“定时开赛”规则详解!
USACO计算机奥赛考试语言是什么?C++、Python、Java选哪个效率最高?

© 2026. All Rights Reserved. 沪ICP备2023009024号-1