/*
 * 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 MyAllgoritmicLib;

import java.util.ArrayList;

public class Dejkstra {
	// ---------------------------------------------------------------------------
	// Àëãîðèòì Äåéêñòðû.ïîèñêà êðàò÷àéøåãî ïóòè

		int v;
	
	   int infinity=Integer.MAX_VALUE;                     // Áåñêîíå÷íîñòü
	   int p= 6;				// Êîëè÷åñòâî âåðøèí â ãðàôå
	   int a[][]={ {0,1,0,0,1,3},  // Ìàòðèöà ñìåæíîñòè ãðàôà
	   {  1,0,5,0,0,1},
	   {            0,5,0,5,20,1},
	   {   0,0,5,0,3,2},
	   {           1,0,20,3,0,10},
	   {  3,1,1,2,10,0  }};

	   // Áóäåì èñêàòü ïóòü èç âåðøèíû s â âåðøèíó g
	   public ArrayList<Integer> search(int s, int g){
		   ArrayList<Integer> result = new ArrayList<Integer>();
	   int x[] = new int[p]; // Ìàññèâ, ñîäåðæàùèé åäèíèöû è íóëè äëÿ êàæäîé âåðøèíû,
	                  // x[i]=0 - åùå íå íàéäåí êðàò÷àéøèé ïóòü â i-þ âåðøèíó,
	                  // x[i]=1 - êðàò÷àéøèé ïóòü â i-þ âåðøèíó óæå íàéäåí
	   int t[] = new int[p];  // t[i] - äëèíà êðàò÷àéøåãî ïóòè îò âåðøèíû s â i
	   int h[] = new int[p];  // h[i] - âåðøèíà, ïðåäøåñòâóþùàÿ i-é âåðøèíå
	   		         // íà êðàò÷àéøåì ïóòè

	   // Èíèöèàëèçèðóåì íà÷àëüíûå çíà÷åíèÿ ìàññèâîâ
	   int u;		    // Ñ÷åò÷èê âåðøèí
	   for (u=0;u<p;u++)
	   {
	      t[u]=infinity; // Ñíà÷àëà âñå êðàò÷àéøèå ïóòè èç s â i
		// ðàâíû áåñêîíå÷íîñòè
	      x[u]=0;        // è íåò êðàò÷àéøåãî ïóòè íè äëÿ îäíîé âåðøèíû
	   }
	   h[s]=0; // s - íà÷àëî ïóòè, ïîýòîìó ýòîé âåðøèíå íè÷åãî íå ïðåäøåñòâóåò
	   t[s]=0; // Êðàò÷àéøèé ïóòü èç s â s ðàâåí 0
	   x[s]=1; // Äëÿ âåðøèíû s íàéäåí êðàò÷àéøèé ïóòü
	   v=s;    // Äåëàåì s òåêóùåé âåðøèíîé
	   
	   while(true)
	   {
	      // Ïåðåáèðàåì âñå âåðøèíû, ñìåæíûå v, è èùåì äëÿ íèõ êðàò÷àéøèé ïóòü
	      for(u=0;u<p;u++)
	      {
	         if(a[v][u]==0)continue; // Âåðøèíû u è v íåñìåæíûå
	         if(x[u]==0 && t[u]>t[v]+a[v][u]) // Åñëè äëÿ âåðøèíû u åùå íå
		// íàéäåí êðàò÷àéøèé ïóòü
	            	// è íîâûé ïóòü â u êîðî÷å ÷åì
		// ñòàðûé, òî
	         {
	            t[u]=t[v]+a[v][u];	// çàïîìèíàåì áîëåå êîðîòêóþ äëèíó ïóòè â
		// ìàññèâ t è
	            h[u]=v;	// çàïîìèíàåì, ÷òî v->u ÷àñòü êðàò÷àéøåãî
		// ïóòè èç s->u
	         }
	      }

	      // Èùåì èç âñåõ äëèí íåêðàò÷àéøèõ ïóòåé ñàìûé êîðîòêèé
	      int w=infinity;  // Äëÿ ïîèñêà ñàìîãî êîðîòêîãî ïóòè
	      v=-1;            // Â êîíöå ïîèñêà v - âåðøèíà, â êîòîðóþ áóäåò
	                       // íàéäåí íîâûé êðàò÷àéøèé ïóòü. Îíà ñòàíåò
	                       // òåêóùåé âåðøèíîé
	      for(u=0;u<p;u++) // Ïåðåáèðàåì âñå âåðøèíû.
	      {
	         if(x[u]==0 && t[u]<w) // Åñëè äëÿ âåðøèíû íå íàéäåí êðàò÷àéøèé
	                               // ïóòü è åñëè äëèíà ïóòè â âåðøèíó u ìåíüøå
	                               // óæå íàéäåííîé, òî
	         {
	            v=u; // òåêóùåé âåðøèíîé ñòàíîâèòñÿ u-ÿ âåðøèíà
	            w=t[u];
	         }
	      }
	      if(v==-1)
	      {
	         return null;
	      }
	      if(v==g) // Íàéäåí êðàò÷àéøèé ïóòü,
	      {        // âûâîäèì åãî
	         System.out.println("Êðàò÷àéøèé ïóòü èç âåðøèíû "+s+" â âåðøèíó "+g+":");
	   	   u=g;
	   	   while(u!=s)
	         {
	   		   result.add(u);
	            System.out.println(" "+u);
	            u=h[u];
	         }
	         System.out.println(" "+s+". Äëèíà ïóòè - "+t[g]);
	         result.add(s);
	         return result;
	      }
	      x[v]=1;
	   }
	   
	}
	/*
	 * Ïðîãðàììà çàïðàøèâàåò âåðøèíû s è q è âûâîäèò êðàò÷àéøèé ïóòü. Íàïðèìåð,
	 * ïîñëå ââîäà s = 3, q = 6, ïðîãðàììà âûâîäèò
	 * 
	 * Íåò ïóòè èç âåðøèíû 3 â âåðøèíó 6.
	 * 
	 * Ïîñëå ââîäà s = 0, q = 2 ïðîãðàììà âûâîäèò
	 * 
	 * Êðàò÷àéøèé ïóòü èç âåðøèíû 0 â âåðøèíó 2: 2 5 1 0. Äëèíà ïóòè = 3.
	 */

	// ---------------------------------------------------------------------------
	 
	   public static void main(String[] args) {
		   Dejkstra dejkstra = new Dejkstra();
		   ArrayList<Integer> result = dejkstra.search(3, 6);
		   result = dejkstra.search(0, 2);
		   result = null;
		   System.out.println(result);
	   }
    
    }
