Sei sulla pagina 1di 2

package wordmagic;

import java.io.File;
import java.util.*;

import lists.Cerializer;

public class Brain {


private HashMap <String, String> map = new HashMap <String, String>(
82000);
private ArrayList <String> sortedKeys = new ArrayList <String>(
82000);

public Brain() {
File file = new File("Words");
String str;
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
str = sc.nextLine();
if (!str.contains(" ") && !str.contains("-")
&& !str.contains("\'") && str.length() > 2
&& !str.contains(".") && !str.contains(":")
&& !str.contains("/")) {
// && !sortedKeys.contains(str.substring(26))) {
map.put(str.substring(26), str.substring(0, 26));
sortedKeys.add(str.substring(26));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public ArrayList <String> findHiddenWords(String str) {


if (str.contains(":") || str.contains(";")
|| str.contains("\"") || str.contains("\'")
|| str.contains("/") || str.contains("\\")
|| str.contains(".") || str.contains(",")
|| str.contains("?") || str.contains("!"))
return null;
int[] ary = stringToIntAry(Cerializer.cerialization(str)), temp;
Set <String> list = new TreeSet <String>();
ArrayList <String> full = new ArrayList <String>();
boolean b = true;
int size = 0;
for (String s : sortedKeys) {
if (s.length() > size) {
size = s.length();
for (String string : list)
full.add(string);
list.clear();
}
if (s.length() > str.length())
return full;
b = true;
temp = stringToIntAry(map.get(s));
for (int i = 0; i < 26; i++) {
if (temp[i] > ary[i])
b = false;
}
if (b)
list.add(s);
}
return full;
}

private int[] stringToIntAry(String str) {


int[] ary = new int[26];
char[] ch = str.toCharArray();
for (int i = 0; i < 26; i++)
ary[i] = Character.getNumericValue(ch[i]);
return ary;
}

public static void main(String[] args) {


Brain b = new Brain();
System.out.println(b.findHiddenWords("integrate"));
}
}

Potrebbero piacerti anche