Arrays 查找由数组中的其他单词组成的数组中最长的单词
我有一个单词数组,需要找到数组中最长的单词,该数组由该数组中的其他单词组成。例如:Arrays 查找由数组中的其他单词组成的数组中最长的单词,arrays,algorithm,find,Arrays,Algorithm,Find,我有一个单词数组,需要找到数组中最长的单词,该数组由该数组中的其他单词组成。例如: String[] mass = { "five", "fivetwo", "fourfive", "fourfivetwo", "one", "onefiveone", "two", "twofivefourone"
String[] mass = {
"five",
"fivetwo",
"fourfive",
"fourfivetwo",
"one",
"onefiveone",
"two",
"twofivefourone"
};
结果应该是“fourfivetwo”->“fourfivetwo”和“two”。你能帮我找到算法吗?存在使用字符串匹配算法KMP和图形的解决方案 算法: 1) 反复读单词。将每个单词设置为“主单词”,即您要构建的单词。对每个这样的单词执行以下步骤 2) 您修复了一个单词,我们将其命名为W。现在再次迭代所有单词,并运行KPM将它们与W进行比较。现在您可以在单词W的字母上构建一个图形。让我们对示例进行解释:
W = "abacdeeee"
Other_word = ["aba", "cde", "eee"]
Word "aba" would connect letter 1 in word W to letter 4.
Word "cde" would connect 4 to 7.
Word "eee" would connect 7 to 9.
Each letter in W is node and other words will make edges.
If there exists a path between first and last letter, which you can
check using BFS/DFS, you can build word W out of other words.
3) 对每个单词重复这个过程,并从其他单词中选择最长的单词
时间复杂性:
假设N是字数,L是平均长度
对于一个单词,您使用其他单词运行KMP,这将需要O(N*(L+L))。构建图在最坏情况下取O(N^2),BFS也是如此。在最坏的情况下,对于每个单词W,您花费O(NL+N^2),但边的数量很可能与N成正比,所以平均值是O(NL)
当您需要为每个N计算上述值时,您将得到以下结果:
最差复杂度:O(N^2*L+N^3)
平均复杂度:O(N^2*L)谢谢大家,我已经决定,如果有人感兴趣,请看下面的代码。我为它感到羞耻,但它起作用了
public class MyMap {
private Map<String, ArrayList<Pair>> map = new TreeMap<String, ArrayList<Pair>>();
private String[] key;
// create special map
//key = element our array
//value = pair elemene
// string = word which contains in this key
// int = count this word (which contains in this key)
public MyMap(String[] ArrayWord) {
key = ArrayWord;
//init arraylist
for (int i = 0; i < ArrayWord.length; i++) {
map.put(ArrayWord[i], new ArrayList<Pair>());
}
//find word which containing key
/*
example:
String[] mass = {
"f",
"five",
"fivetwo",
"one",
"onefiveone",
"two"
};
map[0] => f->[]
map[1] => five->[(f:1)]
map[2] => fivetwo->[(f:1)(five:1)(two:1)]
map[3] => one->[]
map[4] => onefiveone->[(f:1)(five:1)(one:2)]
map[5] => two->[]*/
for (int i = 0; i < ArrayWord.length; i++) {
for (int j = 0; j < ArrayWord.length; j++) {
if (i != j) {
int count = 0;
if (ArrayWord[i].contains(ArrayWord[j])) {
String str = ArrayWord[i];
// find count word which contains in this key
while (str.contains(ArrayWord[j])) {
str = str.replaceFirst(ArrayWord[j], "-");
count++;
}
Pair help = new Pair(ArrayWord[j], count);
map.get(ArrayWord[i]).add(help);
}
}
}
}
}
public String getCompoundWord() {
String word = "";
//check have we compound word or not
if (isHaveCompoundWords()) {
/*remove Unique Elements of the words are found*/
deleteContainingWord();
/* find index element*/
int index = findIndexCompoundWord();
//return -1 if we have no word which compound just with other words array
try {
word = key[findIndexCompoundWord()];
return word;
} catch (IndexOutOfBoundsException ex) {
System.out.println("Have no word which compound just with other words array");
}
} else {
return "Have no word which compound with other words array, just unique element";
}
return key[findIndexCompoundWord()];
}
private void deleteContainingWord() {
/*
update map
after procedure we would have map like this
String[] mass = {
"f",
"five",
"fivetwo",
"one",
"onefiveone",
"two"
};
map[0] => f->[]
map[1] => five->[(f:1)]
map[2] => fivetwo->[(f:1)(ive:1)(two:1)]
map[3] => one->[]
map[4] => onefiveone->[(f:1)(ive:1)(one:2)]
map[5] => two->[]
*/
for (int i = 0; i < map.size(); i++) {
if (map.get(key[i]).size() > 0) {
ArrayList<Pair> tmp = map.get(key[i]);
for (int j = tmp.size() - 1; j >= 0; j--) {
for (int k = tmp.size() - 1; k >= 0; k--) {
if (k != j) {
// if word contains other word2, remove part word
if (tmp.get(j).getName().contains(tmp.get(k).getName())) {
String s = tmp.get(j).getName().replace(tmp.get(k).getName(), "");
tmp.get(j).setName(s);
}
}
}
}
map.put(key[i], tmp);
}
}
}
private int findIndexCompoundWord() {
int indexMaxCompaneWord = -1;
int maxCompaneWordLenght = 0;
for (int i = 0; i < map.size(); i++) {
if (map.get(key[i]).size() > 0) {
ArrayList<Pair> tmp = map.get(key[i]);
int currentWordLenght = 0;
for (int j = 0; j < tmp.size(); j++) {
if (!tmp.get(j).getName().isEmpty()) {
currentWordLenght += tmp.get(j).getName().length() * tmp.get(j).getCount();
}
}
if (currentWordLenght == key[i].length()) {
if (maxCompaneWordLenght < currentWordLenght) {
maxCompaneWordLenght = currentWordLenght;
indexMaxCompaneWord = i;
}
}
}
}
return indexMaxCompaneWord;
}
private boolean isHaveCompoundWords() {
boolean isHaveCompoundWords = false;
for (int i = 0; i < map.size(); i++) {
if (map.get(key[i]).size() > 0) {
isHaveCompoundWords = true;
break;
}
}
return isHaveCompoundWords;
}
公共类MyMap{
私有映射Map=newtreemap();
私有字符串[]密钥;
//创建特殊地图
//key=数组中的元素
//值=对榄香烯
//字符串=此键中包含的单词
//int=计算这个单词(包含在这个键中)
公共MyMap(字符串[]ArrayWord){
键=数组字;
//初始阵列列表
for(int i=0;if->[]
地图[1]=>5->[(f:1)]
地图[2]=>fivetwo->[(f:1)(五:1)(二:1)]
地图[3]=>1->[]
地图[4]=>onefiveone->[(f:1)(five:1)(1:2)]
地图[5]=>2->[]*/
for(int i=0;if->[]
地图[1]=>5->[(f:1)]
地图[2]=>fivetwo->[(f:1)(ive:1)(2:1)]
地图[3]=>1->[]
map[4]=>onefiveone->[(f:1)(ive:1)(one:2)]
地图[5]=>2->[]
*/
对于(int i=0;i0){
ArrayList tmp=map.get(键[i]);
对于(int j=tmp.size()-1;j>=0;j--){
对于(int k=tmp.size()-1;k>=0;k--){
如果(k!=j){
//如果word包含其他word 2,请删除部分word
if(tmp.get(j).getName()包含(tmp.get(k).getName()){
字符串s=tmp.get(j).getName().replace(tmp.get(k).getName(),“”);
tmp.get(j).setName(s);
}
}
}
}
map.put(键[i],tmp);
}
}
}
私有int findIndexCompoundWord(){
int indexMaxCompanyWord=-1;
int maxCompaneWordLenght=0;
对于(int i=0;i0){
ArrayList tmp=map.get(键[i]);
int currentWordLength=0;
对于(int j=0;j0){
isHaveCompoundWords=真;
打破
}
}
返回isHaveCompoundWords;
}