(UVA) 458 - The Decoder - Solução

1) A princípio tentei resolver este problema utilizando o Map para fazer as correspondências, mas o resultado foi "Time limit exceeded".
2) Pesquisando, encontrei a postagem http://www.loiane.com/2011/11/uva-problema-458-the-decoder/ que mostra uma forma diferente de resolver o problema sem precisar usar o Map (o que ajuda muito na redução do tempo) e usando o DataInputStream e o DataOutputStream.

import java.io.*;
import java.util.*;
import java.lang.Math;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.takeInput();
      
        System.exit(0);
    }
  
    void takeInput() throws NumberFormatException, IOException {
        DataInputStream dis = new DataInputStream(System.in);
        DataOutputStream dos = new DataOutputStream(System.out);
      
        byte c;
      
        byte leByte;
        try {
            while (true) {
                leByte = dis.readByte();
                if (leByte != 10 && leByte != 13) {
                    c = (byte)(leByte-7);
                }
                else {
                    c = leByte;
                }
                dos.write(c);
            }           } catch (EOFException eof) {
      
        }

        dos.flush(); // com o flush() no final, é possível reduzir o tempo
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução