import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class ReadFile {
	public static void main(String[] args) throws Exception {
		String srcFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt1.in.txt";
		String destFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt0.out";
		File file = new File(srcFile);
		List<String> readData = new ArrayList<String>();
		FileReader fileInputStream = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileInputStream);
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			readData.add(line);
		}
		stringBuilder.append(generateOutput(readData));
		File writeFile = new File(destFile);
		if (!writeFile.exists()) {
			writeFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(writeFile);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(stringBuilder.toString());
		bufferedWriter.close();
		System.out.println("output" + stringBuilder);
	}

	/**
	 * @param number
	 * @return
	 */
	private static String generateOutput(List<String> number) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < number.size(); i++) {
			if (i == 0) {
				continue;
			} else {
				String tempString = number.get(i);
				String[] temArr = tempString.split(" ");
				stringBuilder.append("Case #" + i + ": ");
				stringBuilder.append(getRecNumbers(temArr[0], temArr[1]));
				stringBuilder.append("\n");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	// /* This method accepts method for google code jam */
	// private static String method(String value) {
	// int intValue = Integer.valueOf(value);
	// double givenExpr = 3 + Math.sqrt(5);
	// double num = Math.pow(givenExpr, intValue);
	// String valArr[] = (num + "").split("\\.");
	// int finalVal = Integer.valueOf(valArr[0]) % 1000;
	//
	// return String.format("%1$03d", finalVal);
	//
	// }
	//
	// /* This method accepts method for google code jam */
	// private static String method2(String value) {
	// StringTokenizer st = new StringTokenizer(value, " ");
	// String test[] = value.split(" ");
	// StringBuffer str = new StringBuffer();
	//
	// for (int i = 0; i < test.length; i++) {
	// // test[i]=test[test.length-i-1];
	// str.append(test[test.length - i - 1]);
	// str.append(" ");
	// }
	// str.deleteCharAt(str.length() - 1);
	// String strReversedLine = "";
	//
	// while (st.hasMoreTokens()) {
	// strReversedLine = st.nextToken() + " " + strReversedLine;
	// }
	//
	// return str.toString();
	// }

	private static int getRecNumbers(String no1, String no2) {

		int a = Integer.valueOf(no1);
		int b = Integer.valueOf(no2);
		Set<String> dupC = new HashSet<String>();
		for (int i = a; i <= b; i++) {
			int n = i;
			List<String> value = findPerm(n);
			for (String val : value) {
				if (Integer.valueOf(val) <= b && Integer.valueOf(val) >= a
						&& n < Integer.valueOf(val)) {
					dupC.add(n + "-" + val);
				}
			}
		}
		return dupC.size();
	}

	private static List<String> findPerm(int num) {
		String numString = String.valueOf(num);
		List<String> value = new ArrayList<String>();
		for (int i = 0; i < numString.length(); i++) {
			String temp = charIns(numString, i);
			if (!temp.equalsIgnoreCase(numString)) {
				value.add(charIns(numString, i));
			}
		}
		return value;

	}

	public static String charIns(String str, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return end + begin;
	}
}
package info.m3o.gcj2012.recyclednumber;

public class RecycledNumber {
	protected String numStr;
	protected int numDigits;

	protected int minA, maxB;

	public RecycledNumber(String s, int mina, int maxb){
		numStr = new String(s);
		numDigits = s.length();
		minA = mina;
		maxB = maxb;
	}

	//	public RecycledNumber(String s){
	//		numStr = new String(s);
	//		numDigits = s.length();
	//	}

	public RecycledNumber(int p, int mina, int maxb){
		numStr = new String(String.valueOf(p));
		numDigits = numStr.length();
		minA = mina;
		maxB = maxb;
	}

	//	public RecycledNumber(int p){
	//		numStr = new String(String.valueOf(p));
	//		numDigits = numStr.length();
	//	}

	public static String recycleAt(String s, int i){
		assert ((i>0) && (i<s.length())) : "int parameter is greater than length.";
		return ( new String (s.substring(i) + s.substring(0, i)));
	}

	public static String[] getCycledNumber(String s){
		String [] str;
		str = new String[s.length()-1];
		
		for(int i=1; i < s.length(); i++){
		 // str[i-1] = new String (recycleAt(s, i));
			str[i-1] = recycleAt(s, i);
		}
		return str;
	}
	

	public void addRecycledNumbers(MyNumberStringPairSet recycledNumberSet,
			MyNumberStringSet tested,  String sNum2) {
// TODO
		/*
		if (tested.contains(sNum2) == true){
			return;
		}*/

		int intS2 = Integer.valueOf(sNum2);

		if (((this.numStr.compareTo(sNum2)) != 0) && (sNum2.startsWith("0") == false)
				&& (intS2 >= this.minA) && (intS2 <= this.maxB)) {
			recycledNumberSet.add(new MyNumberStringPair(this.numStr, sNum2));
		}
		tested.add(numStr);
		tested.add(sNum2);

	}			


	
	
	public void addRecycledNumbers(MyNumberStringPairSet recycledNumberSet, MyNumberStringSet tested){
		if (tested.contains(numStr) == true){ // This value has been tested already.
			return;  
		}

		for(int i=1; i < this.numDigits; i++){
			String s = new String (recycleAt(numStr, i));
			
			// HERE TODO
			tested.add(s);
			int intS = Integer.valueOf(s);
			
			if ((s.startsWith("0")==false) 
					&& (numStr.compareTo(s) != 0)
					&& (intS >=this.minA) 
					&& (intS <= this.maxB)){
				recycledNumberSet.add(new MyNumberStringPair(s, numStr));
			}
			
//			if (s.startsWith("0")== false){
//				tested.add(s); // Here is the part
//			}
		}
		tested.add(numStr);		

	}



	public static boolean testrecycleAt(){
		boolean retb = false;
		boolean []b = new boolean[4];

		String str1 = "12345";

		String str2 = recycleAt(str1, 1);
		b[0] = ("23451".compareTo(str2)==0);

		str2 = recycleAt(str1, 2);
		b[1] = ("34512".compareTo(str2) == 0);

		str2 = recycleAt(str1, 3);
		b[2] = ("45123".compareTo(str2) == 0);

		str2 = recycleAt(str1, 4);
		b[3] = ("51234".compareTo(str2) == 0);

		retb = (b[0] && b[1] && b[2] && b[3]);
		assert (retb == true) : "something is wrong with recycleAt method.";
		return retb;
	}

	public String getNumStr() {
		return numStr;
	}
	public void setNumStr(String numStr) {
		this.numStr = numStr;
	}
	public int getNumDigits() {
		return numDigits;
	}
	public void setNumDigits(int numDigits) {
		this.numDigits = numDigits;
	}



}
