Java Scanner input optimization

Article directory

foreword

The old stuff before, didn't post~

topic

This is nothing to say, but also from the topic of the Blue Bridge Cup.

Resource constraints

Time limit: 1.0s Memory limit: 256.0MB

Problem Description

JOE has an array A of length n, and it is known that at least half of the elements are the same. Now JOE wants to know the mode of the array.

input format

The first line, a number n.
  The second line, n positive integers, represents the A array.

output format

A number representing the mode of these n numbers.

sample input

5
1 1 2 1 2

Sample output

1

Data size and conventions

30% n <= 10^4, value <= 10^8
  60% n <= 10^7, value <= 10^6
  100% n <= 10^7, value <= 10^8

Originally this question is very simple, just scan it directly.


import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    
    
        public static void main( String[ ] args) {
    
    
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        Long n = scanner.nextLong();
        Long res = 1L;
        int result = scanner.nextInt();
        for(Long i = 1L;i<=n&res<=n;i++){
    
    
            int temp = scanner.nextInt();
            if(res == 0) result = temp;
            res += temp == result?1:-1;
        }
        System.out.println( result);
        }

}

But I'm sorry, 60 points, only 6 out of 10 test cases. The
reason is very simple, that scanner is not good.

The solution is to use a Buffer
-like re-Scanner approach.

In this case, I found a packaged class for me on a competition website


import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;


/** Class for buffered reading int and double values */
class Reader {
    
    
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
    
    
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
    
    
        while (!tokenizer.hasMoreTokens()) {
    
    
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
    
    
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
    
    
        return Double.parseDouble(next());
    }
}

problem solved

So in the title it's like this


import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;


/** Class for buffered reading int and double values */
class Reader {
    
    
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
    
    
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
    
    
        while (!tokenizer.hasMoreTokens()) {
    
    
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
    
    
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
    
    
        return Double.parseDouble(next());
    }
}
public class Main {
    
    
        public static void main( String[ ] args) throws IOException {
    
    
        Reader.init(System.in);
        int n = Reader.nextInt();
        int res = 1;
        int result = Reader.nextInt();
        for(int i = 1;i<=n&res<=n;i++){
    
    
            int temp = Reader.nextInt();
            if(res == 0) result = temp;
            res += temp == result?1:-1;
        }
        System.out.println( result);
        }

}

That's it, the problem is solved, after a long time, the scanner of java can't work! I have been looking for information for a long time, looking at other people's solutions, but after thinking about it, it is impossible to be a problem of the algorithm (I also tried a multi-threaded solution, but it is even worse, over-memory, but also locked) and in any case, I have to read it when I find it. It took millions of times, so I wondered if it was someone else's problem, such as scanner, and the result was a good guy. Sure enough, it is impossible to develop without experience in actual combat!

Guess you like

Origin blog.csdn.net/FUTEROX/article/details/123483699