Problem solution · Java

CCC 2014 S2 - Assigning Partners

CCC 2014 S2 - Assigning Partners: a Java solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Hash-based lookup
Source
CCCSolutions
Length
47 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For CCC 2014 S2 - Assigning Partners, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 47 lines of Java from the credited upstream file ccc14s2.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 1 loop block detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2014 S2 - Assigning Partners · JavaJava
Use this to learn the idea, then write your own version.
//Ivan Li, Markville secondary schoolimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import java.util.StringTokenizer; public class CCC14S2 {    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    static StringTokenizer st1, st2;     public static void main(String[] args) throws IOException {        int N = Integer.parseInt(br.readLine());        Map<String, String> map = new HashMap<>();        st1 = new StringTokenizer(br.readLine());        st2 = new StringTokenizer(br.readLine());        for (int i = 0; i < N; i++) {            String a = st1.nextToken();            String b = st2.nextToken();            if (a.equals(b)){                System.out.println("bad");                return;            }            if (!map.containsKey(a) && !map.containsKey(b)) {                map.put(a, b);                map.put(b, a);            }            else {                if (map.containsKey(a)) {                    if (!map.get(a).equals(b)) {                        System.out.println("bad");                        return;                    }                }                else {                    if (!map.get(b).equals(a)) {                        System.out.println("bad");                        return;                    }                }            }        }        System.out.println("good");    }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗