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

package codejam2012.qr;

import codejam.common.CodeHelper;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Chen Ling
 */
public class ProblemC {

    private static String SMALL_IN_FILE_NAME = "/codejam2012/qr/C-small.in";
    private static String LARGE_IN_FILE_NAME = "/codejam2012/qr/C-large.in";
    private static String SMALL_OUT_FILE_NAME = "/codejam2012/qr/C-small.out";
    private static String LARGE_OUT_FILE_NAME = "/codejam2012/qr/C-large.out";

    public static int solve(int start, int end) {
        int total = 0;
        for (int number = start; number < end + 1; number++) {
            String pairN = String.valueOf(number);
            if (pairN.length() == 1) {
                continue;
            }
            for (int i = 1; i < pairN.length(); i++) {
                String pairM = pairN.substring(i, pairN.length()) + pairN.substring(0, i);
                int pairMI = Integer.parseInt(pairM);
                if (pairMI >= start && pairMI > number && pairMI <= end) {
                    total++;
//                    System.out.print("Pair(" + pairN + ", " + pairM + ")");
//                    System.out.print(", Ture");
//                    System.out.println("");
                }

            }
        }
//        System.out.println("Total: " + total);
        return total;
    }

    public static void main(String[] args) {
//        ProblemC.solve(1111, 2222);
        List<String> smallLinesIn = CodeHelper.loadInputLines(SMALL_IN_FILE_NAME);
        List<String> smallOuts = new ArrayList<String>();
        for (int i = 1; i < smallLinesIn.size(); i++) {
            String[] numbers = smallLinesIn.get(i).split(" ");
            int start = Integer.parseInt(numbers[0]);
            int end = Integer.parseInt(numbers[1]);
            smallOuts.add("Case #" + i + ": " + ProblemC.solve(start, end));
        }
        CodeHelper.writeOutputs(SMALL_OUT_FILE_NAME, smallOuts);
//
//        List<String> largeLinesIn = CodeHelper.loadInputLines(LARGE_IN_FILE_NAME);
//        List<String> largeOuts = new ArrayList<String>();
//        for (int i = 1; i < largeLinesIn.size(); i++) {
//            String[] numbers = largeLinesIn.get(i).split(" ");
//            int start = Integer.parseInt(numbers[0]);
//            int end = Integer.parseInt(numbers[1]);
//            largeOuts.add("Case #" + i + ": " + ProblemC.solve(start, end));
//        }
//        CodeHelper.writeOutputs(LARGE_OUT_FILE_NAME, largeOuts);
    }
}
