Hello This is skanehira from Global Sense Co., Ltd..
This time, I tried to solve AOJ's Binary Search. When I saw the title, I thought it would be difficult It was unexpectedly easy ...
In summary Output how many values in the 4th row are included in the 2nd row. It is the content.
To be honest, I don't need to enter the first and third lines ... I don't want to bother to skip it.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
//skip
scan.nextLine();
//Store the search target in map
Map<String, String> map = new HashMap<String, String>();
for (String num : scan.nextLine().split(" ")) {
map.put(num, "");
}
//skip
scan.nextLine();
//Count if there is a search target
int count = 0;
for (String num : scan.nextLine().split(" ")) {
if (map.containsKey(num)) {
count++;
}
}
System.out.println(count);
}
}
}
for (int i : targetNums) {
for (int j : nums) {
if (i = j) count++;
}
}
Store the 2nd and 4th lines in an array I wondered if I could go by comparing while spinning around. It is inefficient to double the for statement I decided to use map.
The process should be faster if you use the map key as the search target and perform a key check! (Although not verified)
I wonder if there is another more efficient way ...
Recommended Posts