【问题标题】:Time limit exceeded online judge超过时限在线判断
【发布时间】:2016-03-15 10:36:20
【问题描述】:

这就是问题所在:

输入 输入的第一行将包含测试用例的数量 T (1 ≤ T ≤ 50)。以下每个 T 行包含一个长度不超过 80 位的正整数 N。 输出 每个测试用例的输出将是单行,其中包含更大的最小回文 大于或等于输入数字。 样本输入

2
42
321

样本输出

44
323

我在提交代码给在线评委时总是超过时间限制(3秒限制)

  class Main {

static String ReadLn (int maxLg) 
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }


    static boolean isPalandriome(String s){
        String newString = "";
        for(int i =s.length()-1;i >= 0; i--){
            newString += s.charAt(i);
        }
        if(newString.equals(s))
            return true;
        else
            return false;
    }
    public static void main(String[] args) {

            BigInteger entredNumber;
            String input;
            input = Main.ReadLn(10);
            int tests = Integer.parseInt(input);
            List<BigInteger> numbers = new ArrayList<BigInteger>();
            for (int i =0;i<tests;i++)
        {
            input = Main.ReadLn(100);
            entredNumber = new BigInteger(input);
                numbers.add(entredNumber);

        }


            for(int i=0;i<tests;i++){
                BigInteger number = numbers.get(i);

                while(!isPalandriome(String.valueOf(number))){
                    number  = number.add(BigInteger.ONE);
                }
                System.out.println(number);

            }

    }
}

我在我的代码中找不到花费太多时间的地方。

【问题讨论】:

  • 逐一搜索耗时过长,例如输入为10000000000000000000000000000000000000010000000000000000000000000000000000000000。你必须开发更有效的算法。
  • 在循环中连接字符串效率低下。您可以使用 StringBuilder.reverse() 更有效(和正确)地反转字符串。
  • 你能帮我计算一下算法吗?我应该怎么做?
  • @AndyTurner 这将使回文检查更有效,但我认为这不会解决问题。从输入到结果查看每个数字的概念被打破。假设每纳秒十次检查。在三秒钟内,该程序最多可以测试 300 亿个数字,但在输入 80 位数字的情况下还远远不够。

标签: java


【解决方案1】:

最后编码希望你觉得这有帮助花了 0.10 秒

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        CustomReader cr = new CustomReader(1000000);
        int T = cr.nextInt(), fIndex, bIndex, fStartIndex, bStartIndex;
        StringBuilder output = new StringBuilder();
        byte[] input;
        boolean isAppend1 = false;
        for (int i = 0; i < T; i++) {
            input = cr.nextInput();
            fStartIndex = bStartIndex = cr.getCurrInputLength() / 2;
            isAppend1 = false;
            if (cr.getCurrInputLength() % 2 == 0) {
                bStartIndex--;
            }
            fIndex = fStartIndex;
            bIndex = bStartIndex;
            while (input[bIndex] == input[fIndex]) {
                if (bIndex - 1 < 0) {
                    break;
                } else {
                    bIndex--;
                    fIndex++;
                }
            }
            if (input[bIndex] > input[fIndex]) {
                while (bIndex >= 0) {
                    input[fIndex++] = input[bIndex--];
                }
            } else {
                if (input[bStartIndex] < 57) {
                    input[bStartIndex] = (byte) (input[bStartIndex] + 1);
                } else {
                    bIndex = bStartIndex;
                    while (bIndex >= 0 && input[bIndex] == 57) {
                        input[bIndex] = 48;
                        bIndex--;
                    }
                    if (bIndex >= 0) {
                        input[bIndex] = (byte) (input[bIndex] + 1);
                    } else {
                        input[0] = 49;
                        if (fStartIndex != bStartIndex) {
                            input[fStartIndex] = 48;
                            bStartIndex = fStartIndex;
                        } else {
                            input[fStartIndex + 1] = 48;
                            bStartIndex = fStartIndex = fStartIndex + 1;
                        }
                        isAppend1 = true;
                    }
                }
                while (bStartIndex > -1) {
                    input[fStartIndex++] = input[bStartIndex--];
                }
            }
            for (int j = 0; j < cr.getCurrInputLength(); j++) {
                output.append((char) input[j]);
            }
            if (isAppend1) {
                output.append("1");
            }
            output.append("\n");
        }
        System.out.print(output.toString());
        // genInput();
    }

    private static class CustomReader {
        private byte[] buffer;
        private byte[] currInput = new byte[1000000];
        private int currInputLength;
        private int currIndex;
        private int validBytesInBuffer;

        CustomReader(int buffSize) {
            buffer = new byte[buffSize];
        }

        public int nextInt() throws IOException {
            int value;
            byte b;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    break;
                }
            }
            value = b - 48;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    value = (value * 10) + (b - 48);
                } else {
                    break;
                }
            }
            return value;
        }

        public byte[] nextInput() throws IOException {
            byte b;
            this.currInputLength = 0;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    break;
                }
            }
            currInput[currInputLength++] = b;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    currInput[currInputLength++] = b;
                } else {
                    break;
                }
            }
            return this.currInput;
        }

        public int getCurrInputLength() {
            return this.currInputLength;
        }

        private byte getNextByte() throws IOException {
            if (currIndex == buffer.length || currIndex == validBytesInBuffer) {
                validBytesInBuffer = System.in.read(buffer);
                currIndex = 0;
            }
            return buffer[currIndex++];
        }
    }

    public static void genInput() {
        for (int i = 0; i < 100; i++) {
            System.out.println((int) (Math.random() * 1000000000));
        }
    }
} 

【讨论】:

  • 非常感谢,有一个小问题你跳过了第一个输入的数字已经是palandriome的情况。
猜你喜欢
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
相关资源
最近更新 更多