In this problem, we have a series of games where two cows have made various gestures in Hoof, Paper, Scissors. We do not know which gestures map to which items, and we would like to figure out the maximum number of games that the first cow could have won.

Instead of thinking about all the possible ways that the numbers map to the items, we can just directly establish which number wins when two different numbers are paired up. For example, one way to pair up the numbers is to have 1 beat 2, 2 beat 3, and 3 beat 1. The other way to pair up the numbers is to have 2 beat 1, 3 beat 2, and 1 beat 3.

We can count for each of these pairings how many games the first cow wins, and report the maximum.

import java.io.*; import java.util.*; public class hps { public static void main(String[] args) throws IOException { // initialize file I/O BufferedReader br = new BufferedReader(new FileReader("hps.in")); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("hps.out"))); // read in the number of games int n = Integer.parseInt(br.readLine()); // initialize a 2D array to store the number of each ordered pair that shows up int[][] matches = new int[4][4]; for(int i = 0; i < n; i++) { // read in one game StringTokenizer st = new StringTokenizer(br.readLine()); int first = Integer.parseInt(st.nextToken()); int second = Integer.parseInt(st.nextToken()); matches[first][second]++; } // compute the maximum number of wins int maximumWins = matches[1][2] + matches[2][3] + matches[3][1]; if(matches[1][3] + matches[3][2] + matches[2][1] > maximumWins) { maximumWins = matches[1][3] + matches[3][2] + matches[2][1]; } // print the answer pw.println(maximumWins); // close the file pw.close(); } }