import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.BitSet; import java.util.HashMap; import java.util.Scanner; public class Phase5 { public static final String regex = "[a-z]+"; public static BitSet[] seen = { new BitSet(), new BitSet(26), new BitSet(325), new BitSet(2600), new BitSet(14950), new BitSet(65780), new BitSet(230230), new BitSet(657800), new BitSet(1562275), new BitSet(3124550), new BitSet(5311735), new BitSet(7726160), new BitSet(9657700), new BitSet(10400600), new BitSet(9657700), new BitSet(7726160), new BitSet(5311735), new BitSet(3124550), new BitSet(1562275), new BitSet(657800), new BitSet(230230), new BitSet(65780), new BitSet(14950), new BitSet(2600), new BitSet(325), new BitSet(26), new BitSet(1) }; public static HashMap lookup = new HashMap(26); public static void main(String[] args) { initlookup(); String line = null; Scanner s2 = null; for (String filename : args) { try { Scanner scr = new Scanner(new File(filename)); while (scr.hasNextLine()) { line = scr.nextLine(); s2 = new Scanner(line); if (! s2.hasNext(regex)) { continue; } getSets(s2.next(regex), line); } } catch (IOException ioe) { ioe.printStackTrace(); } } } private static void initlookup () { // Actual code goes a-z, reduced for post lookup.put('a', new Integer("1")); lookup.put('b', new Integer("2")); } private static void getSets(String str, String line) { int len = str.length(); int bit = getBit(len, str); if (! seen[len].get(bit)) { seen[len].set(bit); System.out.println(str + "\t" + line); for (StringBuilder set : subsets(str)) { getSets(set.toString(), line); } } } private static ArrayList subsets(String str) { ArrayList subs = new ArrayList(); if (str.length() == 1) { return subs; } for (int i = 0; i < str.length(); ++i) { StringBuilder set = new StringBuilder(str); set.deleteCharAt(i); subs.add(set); } return subs; } private static int binomial(int n, int k) { int c = 1; for (int i = 0; i < k; ++i) { c *= n - i; c /= i + 1; } return c; } private static int getBit(int len, String str) { int sum = 0; String key; for (int i = 0; i < len; ++i) { sum += binomial(lookup.get(str.charAt(i)) - 1, i + 1); } return sum; } }