/*
 * 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);
            }
        }
    }
}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/*********************************************************************
 * FileUtils class of static constants and basic file checking routines.
 * This is the header file of constants and methods that might be
 * used in any and all contexts.
 *
 * Methods:
 * public static void CheckArgs(int countArgs,String [] args,String usage)
 * public static void CloseFile(PrintWriter theFile)
 * public static void CloseFile(Scanner theFile)
 * public static void CloseLogFile()
 * public static PrintWriter PrintWriterOpen(String outFileName)
 * public static PrintWriter PrintWriterOpen(String outFileName)
 * public static Scanner ScannerOpen(String inFileName)
 * public static void SetLogFile(String logFileName)
 * public static void SetLogFile(PrintWriter logFile)
 *
 * @author Duncan Buell
 * @version 1.00 Copyright Duncan A. Buell 1 May 2011
**/
public class FileUtils
{
  private static final String TAG = "FileUtils: ";

  public static PrintWriter logFile = null;

/*********************************************************************
 * Accessor methods
**/
/*********************************************************************
 * Mutator methods
**/

/*********************************************************************
 * General methods
**/
/*********************************************************************
 * Method to check the number of arguments to the main.
 *
 * @param countArgs the number of required arguments
 * @param args the argument list at the time the class is invoked
 * @param usage descriptions of the required arguments
**/
  public static void CheckArgs(int countArgs,String [] args,String usage)
  {
    String message;
    if(countArgs > args.length)
    {
      if(1 == countArgs)
      {
        message  = "ERROR: required " + countArgs +
                   " (" + usage + ") arg not present";
      }
      else
      {
        message  = "ERROR: required " + countArgs +
                   " (" + usage + ") args not present";
      }
      FileUtils.logFile.printf("%s%n",message);
      FileUtils.logFile.flush();
      System.exit(1);
    }
  } // public static void checkArgs(int countArgs,String [] args,String usage)

/*********************************************************************
 * Method to close a PrintWriter file.
 * @param theFile the <code>PrintWriter</code> file to close.
**/
  public static void CloseFile(PrintWriter theFile)
  {
//    FileUtils.logFile.printf("%s enter (PrintWriter) CloseFile%n",TAG);
    theFile.flush();
    theFile.close();
//    FileUtils.logFile.printf("%s leave (PrintWriter) CloseFile%n",TAG);
  } // public static void CloseFile(PrintWriter theFile)

/*********************************************************************
 * Method to close a Scanner file.
 * @param theFile The <code>Scanner</code> file to close.
**/
  public static void CloseFile(Scanner theFile)
  {
//    FileUtils.logFile.printf("%s enter (Scanner) CloseFile%n",TAG);
    theFile.close();
//    FileUtils.logFile.printf("%s leave (Scanner) CloseFile%n",TAG);
  } // public static void CloseFile(Scanner theFile)

/*********************************************************************
 * Method to close the PrintWriter class log file.
**/
  public static void CloseLogFile()
  {
//    FileUtils.logFile.printf("%s enter (PrintWriter) CloseLogFile%n",TAG);
    FileUtils.logFile.flush();
    FileUtils.logFile.close();
//    FileUtils.logFile.printf("%s leave (PrintWriter) CloseLogFile%n",TAG);
  } // public static void CloseLogFile()

/*********************************************************************
 * PrintWriterOpen method to open a file as a PrintWriter.
 * 
 * The main purpose of this method is to do the error checking in
 * a subordinate method so as not to clutter up the code flow
 * in methods that have to open files.
 * 
 * @param  outFileName the <code>String</code> name of the file to open
 * @return The opened <code>PrintWriter</code> known to be not null
**/
  public static PrintWriter PrintWriterOpen(String outFileName)
  {
    PrintWriter localPrintWriter = null;

//    FileUtils.logFile.printf("%s enter PrintWriterOpen%n",TAG);

    if(outFileName.equals("System.out"))
    {
      localPrintWriter = new PrintWriter(System.out);
    }
    else
    {
      try
      {
        localPrintWriter = new PrintWriter(new File(outFileName));
      }
      catch (FileNotFoundException fileException)
      {
        FileUtils.logFile.println(TAG + "FILE ERROR opening outFile " +
                                         outFileName);
        FileUtils.logFile.println(fileException.getMessage());
        FileUtils.logFile.println("in" + System.getProperty("user.dir"));
        FileUtils.logFile.flush();
        System.exit(1);
      }
      catch (SecurityException secException)
      {
        FileUtils.logFile.println(TAG + "SECURITY ERROR opening outFile " +
                                         outFileName);
        FileUtils.logFile.println(secException.getMessage());
        FileUtils.logFile.println("in" + System.getProperty("user.dir"));
        FileUtils.logFile.flush();
        System.exit(1);
      }
    }

//    FileUtils.logFile.printf("%s leave PrintWriterOpen%n",TAG);
    return localPrintWriter;
  } // public static PrintWriter PrintWriterOpen(String outFileName)

/*********************************************************************
 * ScannerOpen method to open a file as a Scanner.
 * 
 * The main purpose of this method is to do the error checking in
 * a subordinate method so as not to clutter up the code flow
 * in methods that have to open files.
 * 
 * @param  inFileName the <code>String</code> name of the file to open
 * @return The opened <code>Scanner</code> known to be not null
**/
  public static Scanner ScannerOpen(String inFileName)
  {
    Scanner localScanner = null;

//    FileUtils.logFile.printf("%s enter ScannerOpen%n",TAG);
    if(inFileName.equals("System.in"))
    {
      localScanner = new Scanner(System.in);
    }
    else
    {
      try
      {
        localScanner = new Scanner(new File(inFileName));
      }
      catch (FileNotFoundException ex)
      {
        FileUtils.logFile.println("TAG + ERROR opening inFile " + inFileName);
        FileUtils.logFile.println(ex.getMessage());
        FileUtils.logFile.println("in" + System.getProperty("user.dir"));
        FileUtils.logFile.flush();
        System.exit(1);
      }
    }

//    FileUtils.logFile.printf("%s leave ScannerOpen%n",TAG);
    return localScanner;
  } // public static Scanner ScannerOpen(String inFileName)

/*********************************************************************
 * Method to set the logFile given the name of the file.
 *
 * @param logFileName the <code>String</code> name of the log file.
**/
  public static void SetLogFile(String logFileName)
  {
    FileUtils.logFile = FileUtils.PrintWriterOpen(logFileName);
  }

/*********************************************************************
 * Method to set the logFile given the <code>PrintWriter</code> file.
 *
 * @param logFile the <code>PrintWriter</code> log file.
**/
  public static void SetLogFile(PrintWriter logFile)
  {
    FileUtils.logFile = logFile;
  }

/*********************************************************************
 * Method to convert a string to a double with error checking.
 *
 * @param ss the <code>String</code> value to convert.
 * @param value the default value for an empty string.
 * @return the converted value as a <code>Double</code>.
**/
  static public Double StringToDouble(String ss, Double value)
  {
    Double returnValue;

    returnValue = value;
    ss = ss.trim();
    if(0 != ss.length())
    {
//    FileUtils.logFile.printf("nonempty string %s%n", ss);
      returnValue = Double.valueOf(ss);
    }

    return returnValue;
  } // static public Double stringToDouble(String s, Double value)

/*********************************************************************
 * Method to convert a string to an integer with error checking.
 *
 * @param ss the <code>String</code> value to convert.
 * @param value the default value for an empty string.
 * @return the converted value as an <code>Integer</code>.
**/
  static public Integer StringToInteger(String ss, Integer value)
  {
    Integer returnValue;

    returnValue = value;
    ss = ss.trim();
    if(0 != ss.length())
    {
//    FileUtils.logFile.printf("nonempty string %s%n", ss);
      returnValue = Integer.valueOf(ss);
    }

    return returnValue;
  } // static public Integer stringToInteger(String s, Integer value)

} // public class FileUtils
