- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- 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.
Use this to learn the idea, then write your own version.
12import java.io.BufferedReader;3import java.io.IOException;4import java.io.InputStreamReader;5import java.util.HashMap;6import java.util.Map;7import java.util.StringTokenizer;8 9public class CCC14S2 {10 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));11 static StringTokenizer st1, st2;12 13 public static void main(String[] args) throws IOException {14 int N = Integer.parseInt(br.readLine());15 Map<String, String> map = new HashMap<>();16 st1 = new StringTokenizer(br.readLine());17 st2 = new StringTokenizer(br.readLine());18 for (int i = 0; i < N; i++) {19 String a = st1.nextToken();20 String b = st2.nextToken();21 if (a.equals(b)){22 System.out.println("bad");23 return;24 }25 if (!map.containsKey(a) && !map.containsKey(b)) {26 map.put(a, b);27 map.put(b, a);28 }29 else {30 if (map.containsKey(a)) {31 if (!map.get(a).equals(b)) {32 System.out.println("bad");33 return;34 }35 }36 else {37 if (!map.get(b).equals(a)) {38 System.out.println("bad");39 return;40 }41 }42 }43 }44 System.out.println("good");45 }46}47