(UVA) Do the Untwist - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=582



import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int key = sc.nextInt();
        while (key != 0) {
            String code = sc.next();
            int size = code.length();

            String s = "_abcdefghijklmnopqrstuvwxyz.";
           
            int[] ciphercode = new int[size];
            // translate the string to ciphercode
            for (int i = 0; i < size; i++) {
                ciphercode[i] = s.indexOf(code.charAt(i));
            }
           
            int[] plaincode = new int[size];
            // translate the ciphercode to plaincode
            for (int i = 0; i < size; i++) {
                plaincode[(key*i)%size] = (ciphercode[i]+(i%size))%28;
            }
           
            // translate the plaincode to the valid string
            for (int i = 0; i < size; i++) {
                bw.write(s.charAt(plaincode[i]));
            }
            bw.write("\n");
           
            key = sc.nextInt();
        }                  

        bw.flush();
        bw.close();
               
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução