首页 > 解决方案 > 即使我使用 BufferedReader 读取文本,我的代码仍然给出超时错误

问题描述

在 E Olymp 任务中,我需要将每个字母 'a' 转换为 'b' 和 'b' 转换为 'a',包括它们在文本中的大写和小写。我找到了几种方法,但它们都给出了超时错误。即使我使用了 BufferedReader(这是读取文本的最快方式),我的代码也无法通过超过 60% 的测试用例。这是我的代码,你能告诉我什么会导致超时错误吗?

第一:

   import java.io.BufferedReader;
     import java.io.BufferedWriter;
     import java.io.FileReader;
     import java.io.FileWriter;
     import java.io.IOException;
     public class Se{
       public static void main(String[] args) throws IOException{
     try {
     BufferedReader aze = new BufferedReader(new FileReader("input.txt"));
     BufferedWriter baze = new BufferedWriter(new FileWriter("output.txt"));
      String u;
     String w = "";
     while((u=aze.readLine())!=null) {
        w += u + System.lineSeparator();
    }
    String a = w.replaceAll("a","1");
    String c = a.replaceAll("b","a");
    String d = c.replaceAll("1","b");
    String e = d.replaceAll("A","1");
    String f = e.replaceAll("B","A");
    String G = f.replaceAll("1","B");
    baze.write(G);
    baze.close();
        aze.close();
    }
    catch(Exception a) {
        
    }
    }
}

第二个:

     import java.io.*;
     public class Task{
     public static void main(String[] args) throws IOException{
     try {
     BufferedReader aze = new BufferedReader(new FileReader("input.txt"));
     BufferedWriter baze = new BufferedWriter(new FileWriter("output.txt"));
     String u;
     String w = "";
     while((u=aze.readLine())!=null) {
        w += u + System.lineSeparator();
     }
     String h = "";
     String a = "a";
     String b = "b";
     String A = "A";
    String B = "B";
    for(int i = 0; i < w.length(); i++) {
        String t = "" + w.charAt(i);
        if((t).equals(a)) {
            h += b;
        }
        else if(t.equals(b)) {
            h += a;
        }
        else if(t.equals(A)) {
            h += B;
        }
        else if(t.equals(B)) {
            h += A;
        }
        else {
            h += t;
        }
    }
    baze.write(h);
    baze.close();
        aze.close();
    }
    catch(Exception a) {
        
    }
    }
}

第三个:

  import java.io.*;
   public class Task{
  public static void main(String[] args) throws IOException{
    try {
    BufferedReader aze = new BufferedReader(new FileReader("input.txt"));
    BufferedWriter baze = new BufferedWriter(new FileWriter("output.txt"));
    String u;
    String w = "";
    while((u=aze.readLine())!=null) {
        w += u + System.lineSeparator();
    }
    String h = "";
    String a = "abAB";
    for(int i = 0; i < w.length(); i++) {
        for(int j = 0; j < a.length(); j++) {
            Character ch= w.charAt(i);
        if(ch.equals('a') || ch.equals('A')) {
            h += (char)((int)w.charAt(i)+1);
            j = a.length()-1;
        }
        else if(ch.equals('b') || ch.equals('B')) {
            h += (char)((int)w.charAt(i)-1);
            j = a.length()-1;
        }
        else {
        h += w.charAt(i);
        j = a.length()-1;
        }
    }
    }
    baze.write(h);
    baze.close();
        aze.close();
    }
    catch(Exception a) {
        
    }
    }
}

标签: java

解决方案


w += u + System.lineSeparator();

这是非常低效的。StringBuilder是你如何有效地连接字符串,但一般来说,这些练习不应该首先涉及连接。阅读足够做一份工作,然后去做。

同样的原则适用于h

您正在阅读所有内容(以非常低效的方式),然后循环输入,将所需的输出写入另一个字符串(再次以非常低效的方式),然后一次将整个内容写出来。

取而代之的是,尽可能多地阅读以生成一些输出,然后(立即)写入,然后继续进行,直到输入用完为止。


推荐阅读