You are viewing a single comment's thread. Return to all comments →
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.TreeMap; public class SparseArrays { public static void main(String[] args) { try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))){ TreeMap<String, Integer> map = new TreeMap<>(); int n = Integer.parseInt(br.readLine()); for(int i = 0; i < n; i++){ String line = br.readLine(); map.put(line, map.getOrDefault(line, 0) + 1); } int q = Integer.parseInt(br.readLine()); for(int i = 0; i < q; i++){ String line = br.readLine(); bw.write(map.getOrDefault(line, 0) + "\n"); } bw.flush(); } catch (Exception e) { throw new RuntimeException(e); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →