// CodeEval String List
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
class Main {
public static void main(String[] args) {
string_list.main(args);
}
}
class string_list {
public static void main(String[] args) {
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
if (LOCAL_TEST)
CodeEvalGetInput("e:\\zin.txt");
else
CodeEvalGetInput(args[0]);
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
Initialize();
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split(",");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Initialize() {
}
private static void Process(String[] lineArray) {
Integer n = Integer.valueOf(lineArray[0]);
String s0 = lineArray[1];
char[] cs0 = s0.toCharArray();
Set<Character> cset = new TreeSet<Character>();
for (int i = 0; i < cs0.length; i++) {
cset.add(cs0[i]);
}
String s = "";
for (Character c : cset) {
s += c.toString();
}
Integer slen = s.length();
Set<String> set = new LinkedHashSet<String>();
FillSet("", s, set, n);
String[] ss = set.toArray(new String[set.size()]);
for (int i = 0; i < ss.length; i++) {
if (i > 0)
System.out.print(",");
System.out.print(ss[i]);
}
System.out.println();
}
private static void FillSet(String pre, String s, Set<String> set, int n) {
if (pre.length() == n) {
set.add(pre);
}
else {
for (int i = 0; i < s.length(); i++) {
String s2 = pre + s.substring(i, i + 1);
FillSet(s2, s, set, n);
}
}
}
}
No comments:
Post a Comment