/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam;

/**
 *
 * @author eblanco
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.JFileChooser;

public class RecycledNumbers {

    public static String DatosArchivo[] = new String[10000];

    public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
        if (sarray != null) {
            int intarray[] = new int[sarray.length];
            for (int i = 0; i < sarray.length; i++) {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
            return intarray;
        }
        return null;
    }

    public static boolean CargarArchivo(String arch) throws FileNotFoundException, IOException {
        FileInputStream arch1;
        DataInputStream arch2;
        String linea;
        int i = 0;

        if (arch == null) {
            //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
            int rc = fc.showDialog(null, "Select a File");
            if (rc == JFileChooser.APPROVE_OPTION) {
                arch = fc.getSelectedFile().getAbsolutePath();
            } else {
                System.out.println("No hay nombre de archivo..Yo!");
                return false;
            }
        }
        arch1 = new FileInputStream(arch);
        arch2 = new DataInputStream(arch1);
        do {
            linea = arch2.readLine();
            DatosArchivo[i++] = linea;
            /* if (linea != null) {
            System.out.println(linea);
            }*/
        } while (linea != null);
        arch1.close();
        return true;
    }

    public static boolean GuardaArchivo(String arch, String[] Datos) throws FileNotFoundException, IOException {
        FileOutputStream out1;
        DataOutputStream out2;
        int i;
        out1 = new FileOutputStream(arch);
        out2 = new DataOutputStream(out1);

        for (i = 0; i < Datos.length; i++) {
            if (Datos[i] != null) {
                out2.writeBytes(Datos[i] + "\n");
            }
        }
        out2.close();
        return true;
    }

    public static void echo(Object msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) throws IOException, Exception {
        String[] res = new String[10000], num = new String[2];
        int i, j, k, ilong;
        int ele = 1;
       ArrayList al = new ArrayList();
        String FName = "C-small-attempt0",  istr, irev, linea;
        if (CargarArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".in")) {
            for (j = 1; j <= Integer.parseInt(DatosArchivo[0]); j++) {
                linea = DatosArchivo[ele++];
                num = linea.split(" ");
                   al.clear();
                // echo("A: "+num[0]+" B: "+num[1]);
                for (i = Integer.parseInt(num[0]); i <= Integer.parseInt(num[1]); i++) {
                    istr = Integer.toString(i);
                    ilong = istr.length();
                 
                    for (k = 1; k < ilong; k++) {
                        if (ilong > k) {
                            irev = istr.substring(istr.length() - k, istr.length()) + istr.substring(0, istr.length() - k);
                            //echo("caso: " + j + ": isrt: " + istr + " irev: " + irev);
                            if ((Integer.parseInt(irev) > Integer.parseInt(num[0])) && (Integer.parseInt(irev) > Integer.parseInt(istr)) && (Integer.parseInt(irev) <= Integer.parseInt(num[1]))) {
                                al.add("(" + istr + "," + irev + ")");
                            }
                        }
                    }
                }
                HashSet hs = new HashSet();
                hs.addAll(al);
                al.clear();
                al.addAll(hs);
                res[j] = "Case #" + j + ": " + al.size();
                echo(res[j]);
                LeerArchivo.GuardaArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".out", res);
            }
        }
    }
}

package com.codegem.zaidansari;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class ProblemA {

    public static Map<Character, Character> mappedCharacters = new TreeMap<Character, Character>();
    public static Set<Character> validCharacters = new HashSet<Character>();

    static {
        int startIndex = 97, endIndex = 122;
        // Fill mapped with all required mapping character with only keys populated
        for (int index = startIndex; index <= endIndex; index++) {
            validCharacters.add(Character.valueOf((char) index));
        }

        Map<String, String> sampleEntriesMap = new HashMap<String, String>();
        // Put Given Mapping in Map
        sampleEntriesMap.put("a", "y");
        sampleEntriesMap.put("o", "e");
        sampleEntriesMap.put("z", "q");
        sampleEntriesMap.put("ejp mysljylc kd kxveddknmc re jsicpdrysi", "our language is impossible to understand");
        sampleEntriesMap.put("rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd", "there are twenty six factorial possibilities");
        sampleEntriesMap.put("de kr kd eoya kw aej tysr re ujdr lkgc jv", "so it is okay if you want to just give up");
        loadMap(sampleEntriesMap);
    }

    private static void loadMap(Map<String, String> sampleEntriesMap) {
        Set<Character> populatedCharacters = new HashSet<Character>();

        for (String key : sampleEntriesMap.keySet()) {
            String value = sampleEntriesMap.get(key);
            if (key.length() != value.length()) { throw new RuntimeException("Size of input :" + key + ", and output don't match:" + value); }
            // Read each character from key and if it's not present in mappedCharacters try to fill that
            char[] inputCharacterArray = key.toCharArray();
            char inputCharacter;
            for (int index = 0; index < inputCharacterArray.length; index++) {
                inputCharacter = inputCharacterArray[index];
                if (validCharacters.contains(inputCharacter) && !mappedCharacters.containsKey(inputCharacter)) {
                    mappedCharacters.put(inputCharacter, value.charAt(index));
                    populatedCharacters.add(inputCharacter);
                }
            }
        }

        // Find any missing entry there can be only Zero or one entry missing in population if more than that than it means sample input are not
        // enough
        Set<Character> missingKeys = new HashSet<Character>(validCharacters);
        Set<Character> missingValues = new HashSet<Character>(validCharacters);
        missingKeys.removeAll(populatedCharacters);
        missingValues.removeAll(mappedCharacters.values());
        if (missingKeys.size() > 1) throw new RuntimeException("More than one keys are unresolved using provided samples...");
        if (missingKeys.size() == 1) {
            Character missingKey = missingKeys.iterator().next();
            Character missingValue = missingValues.iterator().next();
            mappedCharacters.put(missingKey, missingValue);
        }
    }

    public static String getEnglishText(String googlereseText) {
        StringBuilder englishText = new StringBuilder(googlereseText);
        Character currentCharacter;
        for (int index = 0; index < englishText.length(); index++) {
            currentCharacter = englishText.charAt(index);
            if (validCharacters.contains(currentCharacter)) {
                englishText.setCharAt(index, mappedCharacters.get(currentCharacter));
            }
        }
        return englishText.toString();
    }

    public static void main(String[] args) {
        File file = new File("/Users/zaansari/Desktop/CodeGem2012/InputFiles/ProblemA.txt");
        List<String> inputData = CodeGemUtil.getInputValues(file);
        int noOfInputs = Integer.parseInt(inputData.get(0));
        inputData.remove(0);

        List<String> outputData = new ArrayList<String>();
        for (int index = 0; index < noOfInputs; index++) {
            outputData.add(ProblemA.getEnglishText(inputData.get(index)));
        }
        boolean createdOutputFile = CodeGemUtil.createOutputFile(outputData, "ProblemA_Output.txt");
        if (createdOutputFile) {
            System.out.println("Successfully created output file");
        } else {
            System.out.println("Failed to create output file");
        }
    }
}
