L1-005. Test Seat Number (Java Time-out) PAT Group Programming Ladder Tournament-Exercise Set

Keywords: Java

Each PAT candidate will be assigned two seat numbers, one for the test machine and the other for the test. Normally, the examinee gets the seat number of the test machine first when he enters the test machine. When he enters the test machine, the system will show the examinee's seat number. When he enters the test machine, he needs to change to the test seat. But some candidates are late and the test machine is over. They can only turn to you for help with the seat number they get and find out their test seat number from the background.

Input format:

Input the first line to give a positive integer N (<=1000), then N lines, each line gives a candidate's information: "admission number test machine seat number test seat number". The admission number is composed of 14 digits and seats are numbered from 1 to N. Enter to ensure that each person's admission number is different, and at no time will two people be assigned to the same seat.
After candidates'information, a positive integer M (<=N) is given, and then M test seat numbers to be queried are given in the next line, separated by spaces.

Output format:

For each seat number that needs to be inquired, the corresponding candidate's admission card number and test seat number are output in one line, separated by a space in the middle.

Input sample:

4
10120150912233 2 4
10120150912119 4 1
10120150912126 1 3
10120150912002 3 2
2
3 4

Output sample:

10120150912002 2
10120150912119 1

The first time I didn't notice it was 200 ms, I simply wrote the lookup, and then the middle two test points timed out.
The second time HashMap matching was used, or the middle two timeouts.
The first two times are using Scanner for input, considering that this may be the problem, so instead of Buffer Reader, once. Upper Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        // Fast Reading Using BufferReader
        BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
        // Read an integer number in a line
        int N = Integer.parseInt(bReader.readLine());
        HashMap<String, String[]> map = new HashMap<String, String[]>();

        while (N-- > 0) {
            // Read a line and divide it with spaces
            String[] line = bReader.readLine().split(" ");
            String[] data = new String[2];
            data[0] = line[0];
            data[1] = line[2];
            // Store data in map
            map.put(line[1], data);
        }

        int M = Integer.parseInt(bReader.readLine());
        // Divide a line of numbers with queries by spaces
        String[] query = bReader.readLine().split(" ");
        for (int i = 0; i < M; i++) {
            String[] data = map.get(query[i]);
            System.out.println(data[0] + " " + data[1]);
        }
    }


    // Two test points in the middle of scanner + hashmap timeout
    public void method2() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        HashMap<Integer, String[]> map = new HashMap<Integer, String[]>();

        while (N-- > 0) {
            String[] data = new String[2];
            data[0] = scanner.next();
            int id = scanner.nextInt();
            data[1] = scanner.next();
            map.put(id, data);
        }

        int M = scanner.nextInt();
        while (M-- > 0) {
            int id = scanner.nextInt();
            String[] data = map.get(id);
            System.out.println(data[0] + " " + data[1]);
        }
    }
}

Posted by cafrow on Wed, 06 Feb 2019 05:03:16 -0800