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

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 *
 * @author malgia
 */
public class Recycled {

    /**
     * @param args the command line arguments
     */
public static void main(String[] args) {
        Integer number=0;
        Integer total=0;
        Integer a,b;
        String numbers[];
        Integer result;
        String current;
        String pair;
        String previous;
        try{
        BufferedReader fin=new BufferedReader(new InputStreamReader(new FileInputStream(new File("C-small-attempt1.in"))));
        PrintWriter fout = new PrintWriter (new BufferedWriter (new FileWriter ("C-small-attempt1.out")));
        total=Integer.parseInt(fin.readLine());
        for (int i=0;i<total;i++){
            result=0;
            previous="1,1";
            numbers=fin.readLine().split(" ");
            a=Integer.parseInt(numbers[0]);
            b=Integer.parseInt(numbers[1]);
            if (b>9) {
            for(int j=a;j<=b;j++){
                current=j+"";
                for(int k=1; k<current.length(); k++){
                    pair=current.substring(k)+current.substring(0,k);
                    if(pair.startsWith("0") || current.startsWith("0")){
                        continue;
                    }
                    if(Integer.parseInt(pair)<=b && Integer.parseInt(pair)>Integer.parseInt(current) && Integer.parseInt(current)>=a && previous.compareTo(current+","+pair)!=0){
                        result++;
                        previous=current+","+pair;
                    }
                }
            }
            }
            number=i+1;
            fout.println("Case #"+number+": "+result);
        }
        fin.close();
        fout.close();
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
}
