Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2012 -- CSC 2700 Section 02

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

week04/e/Plaintext_10896.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;


 /*
 *   Author: Son Pham
 *     Date: September 26, 2012
 *  Purpose: This program finds the key to encrypted words.
 *  Problem: 10896 - B: Known Plaintext Attack
 * Comments: The code is ugly and barely readable. I'll go back and
 *           improve it when I have the time.
 */

class Plaintext {
    
    public static void findKey(String[] e, String d){
        String keys = "";
        for(int j = 0; j < e.length; j++){     
            if (e[j].length() == d.length()){
                int key = 0;
                
                for(int k = 0; k < d.length(); k++){
                    int pkey = key;
                    int che = e[j].charAt(k) - (int)'a' + 1;
                    int chd = d.charAt(k) - (int)'a' + 1;
                    if(chd > che)
                        key = 26 - chd + che;
                    else
                        key = che - chd;
                    if((k > 0) && (pkey != key)){
                        key = -1;
                        break;
                    }
                }
                if(key > -1){
                    key = (int)'a'+key;
                    keys = keys+key+" ";
                }
            }            
        }
        String[] results = keys.split("\\s+");
        int[] skeys = new int[results.length];
        for(int l = 0; l < results.length; l++){
            skeys[l] = Integer.parseInt(results[l]);
        }
        Arrays.sort(skeys);
        for(int m = 0; m < skeys.length; m++){
            System.out.print((char)skeys[m]);
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line = reader.readLine();
            int n = Integer.parseInt(line);
            for(int i = n; n > 0; --n){
                line = reader.readLine();
                String[] en = line.split("\\s+");
                line = reader.readLine();
                String de = line;
                findKey(en,de);
            }
        }
        catch(Exception ex){}
    }
}

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 Isaac Traxler