Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2014 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week04/i/Cryptography.java

/*
 *  Author: Son Pham
 *    Date: February 18, 2013
 * Purpose: This program untwists twisted messages
 * Problem: 641 -Do the Untwist
 */


import java.util.Scanner;

public class Cryptography {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String read = reader.nextLine();
        String[] split = read.split("\\s+");
        char[] plaincode = new char[28];
        initPCode(plaincode);
        int k = Integer.parseInt(split[0]);
        while(k != 0){
            String message = split[1];
            char[] ciphertext = message.toCharArray();
            int n = ciphertext.length;
            char[] plaintext = new char[n];
            for(int i = 0; i < n; i++){
                int c;
                if(ciphertext[i] == '_')
                    c = 0;
                else if(ciphertext[i] == '.')
                    c = 27;
                else
                    c = (int)ciphertext[i] - 96;
                c = (c+i)%28;
                plaintext[(k*i)%n] = plaincode[c];
            }
            String umessage = "";
            for(char u: plaintext)
                umessage += u;
            System.out.println(umessage);
            read = reader.nextLine();
            split = read.split("\\s+");
            k = Integer.parseInt(split[0]);
        }
        
    }
    
    public static void initPCode(char[] plc){
        plc[0] = '_';
        int i = 1;
        char c = 'a';
        while(i < 27){
            plc[i++] = c++;
        }
        plc[27] = '.';
    }
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014