Java OJ 快速读入 竞赛用

Posted licsber

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java OJ 快速读入 竞赛用相关的知识,希望对你有一定的参考价值。

背景

Java打比赛太吃亏了吧
人家C艹有超快的getchar()
最不济cin的效率也比Java带的Scanner高
还有内存占用方面
竞赛中都不计算Java的内存占用
因为占用太多了( 在空间上卡Java一卡一个准

结论

技术图片

利用读入二维数组进行测试:

用StreamTokenizer是最快的方法

代码如下:

public class TokenRead {
    public static int[][] bufferedRead() throws IOException {
        StreamTokenizer scanner = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        scanner.nextToken();
        long initTime = System.currentTimeMillis();
        int n = (int) scanner.nval;
        int[][] all = new int[n][];

        for (int i = 0; i < n; i++) {
            scanner.nextToken();
            int count = (int) scanner.nval;
            all[i] = new int[count];
            for (int j = 0; j < count; j++) {
                scanner.nextToken();
                all[i][j] = (int) scanner.nval;
            }
        }

        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

大概是其他读入方法的数量级的差距

对比算法1:

    public static int[][] bufferedRead(String input) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

        int n = scanner.nextInt();
        long initTime = System.currentTimeMillis();
        int[][] all = new int[n][];
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            all[i] = new int[num];
            for (int j = 0; j < num; j++) {
                int value = scanner.nextInt();
                all[i][j] = value;
            }
        }
        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

对比算法2:

    public static int[][] lineRead(String input) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));

        int n = scanner.nextInt();
        long initTime = System.currentTimeMillis();
        scanner.nextLine();

        int[][] all = new int[n][];
        for (int i = 0; i < n; i++) {
            String num = scanner.nextLine();
            String[] nums = num.split(" ");
            int len = Integer.parseInt(nums[0]);
            all[i] = new int[len];
            for (int j = 0; j < len; j++) {
                all[i][j] = Integer.parseInt(nums[j + 1]);
            }
        }

        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

对比算法3:

    public static int[][] forceRead(String input) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long initTime = System.currentTimeMillis();

        int[][] all = new int[n][];
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            all[i] = new int[num];
            for (int j = 0; j < num; j++) {
                int value = scanner.nextInt();
                all[i][j] = value;
            }
        }
        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

以上是关于Java OJ 快速读入 竞赛用的主要内容,如果未能解决你的问题,请参考以下文章

组队打代码!!!

快读(快速读入)有多快(未完结)

如何从标准输入读入数据--java语言

scu oj 4442 Party(四川省acm程序设计竞赛)

信息学竞赛中的读入比较与其他读入方法

[解题报告] CSDN竞赛第17期