import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
String m = "";
HashSet<String> hs=new HashSet<String>();
while ((m = br.readLine()) != null) {
if ("XXXXXX".equals(m)) {
break;
}
hs.add(m);
}
boolean enter=false;
while ((m = br.readLine()) != null) {
if ("XXXXXX".equals(m)) {
break;
}
char[]arr=m.toCharArray();
boolean[]temp=new boolean[arr.length];
Arrays.sort(arr);
String arrS=new String(arr);
StringBuilder tempSb=new StringBuilder();
permutationsChar(0,arrS,temp,arr,hs,tempSb);
if(tempSb.toString().equals("")){
sb.append("NOT A VALID WORD\n");
}else{
sb.append(tempSb);
}
sb.append("******\n");
}
System.out.print(sb);
}
public static void permutationsChar(int i, String s, boolean[] v, char[] res, HashSet<String> hs,StringBuilder sb) {
if (i == res.length) {
String temp=new String(res);
if(hs.contains(temp)){
sb.append(temp).append("\n");
}
} else {
for (int j = 0; j < s.length(); j++) {
if (!v[j]) {
if ((j > 0 && !v[j - 1] && s.charAt(j - 1) == s.charAt(j))) {
continue;
}
v[j] = true;
res[i] = s.charAt(j);
permutationsChar(i + 1, s, v, res, hs,sb);
v[j] = false;
res[i] = '-';
}
}
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
String m = "";
HashSet<String> hs=new HashSet<String>();
while ((m = br.readLine()) != null) {
if ("XXXXXX".equals(m)) {
break;
}
hs.add(m);
}
boolean enter=false;
while ((m = br.readLine()) != null) {
if ("XXXXXX".equals(m)) {
break;
}
char[]arr=m.toCharArray();
boolean[]temp=new boolean[arr.length];
Arrays.sort(arr);
String arrS=new String(arr);
StringBuilder tempSb=new StringBuilder();
permutationsChar(0,arrS,temp,arr,hs,tempSb);
if(tempSb.toString().equals("")){
sb.append("NOT A VALID WORD\n");
}else{
sb.append(tempSb);
}
sb.append("******\n");
}
System.out.print(sb);
}
public static void permutationsChar(int i, String s, boolean[] v, char[] res, HashSet<String> hs,StringBuilder sb) {
if (i == res.length) {
String temp=new String(res);
if(hs.contains(temp)){
sb.append(temp).append("\n");
}
} else {
for (int j = 0; j < s.length(); j++) {
if (!v[j]) {
if ((j > 0 && !v[j - 1] && s.charAt(j - 1) == s.charAt(j))) {
continue;
}
v[j] = true;
res[i] = s.charAt(j);
permutationsChar(i + 1, s, v, res, hs,sb);
v[j] = false;
res[i] = '-';
}
}
}
}
}
No comments:
Post a Comment