Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写一个程序来查找javascript数组中由其他单词组成的最长单词_Javascript_Algorithm - Fatal编程技术网

编写一个程序来查找javascript数组中由其他单词组成的最长单词

编写一个程序来查找javascript数组中由其他单词组成的最长单词,javascript,algorithm,Javascript,Algorithm,我编写了一个程序来查找数组中由其他单词组成的最长单词 sort_arr.forEach(word => { if (isLargest(word, word)) { console.log(word); } }); function isLargest(initialWord, word) { let first_half = 0; let second_half = 0; let start = 0;

我编写了一个程序来查找数组中由其他单词组成的最长单词

  sort_arr.forEach(word => {
     if (isLargest(word, word)) {
        console.log(word);
     }
   });

    function isLargest(initialWord, word) {
    let first_half = 0;
    let second_half = 0;
    let start = 0;
    let end = 0;
    for (let i = 0; i < word.length; i++) {
        end++;
        first_half = word.substring(start, end);

        for (let j = 0; j < sort_arr.length; j++) {
            if (first_half === sort_arr[j]) {
                second_half = word.substring(end, word.length);
                if(second_half === ''){
                    return word !== initialWord;
                }
                else{
                    return isLargest(initialWord, second_half);
                }
            }
        }

    }
}
它给出输出
null

但是结果应该是
catsdogcats

我知道问题发生在
catsdogcats
中,前缀为
cat
而后缀为
sdogcats
。但是它没有检查前缀
cats
和后缀
dogcats


有人能给我建议一些不使用
领带的方法吗这比最初预期的要复杂一些。你必须看到哪些单词与当前单词的开头相同,并尝试使用其中的每一个单词,直到你得到由其他单词组成的完整单词

const canMakeWord=(数组,单词)=>{
//递归函数
常量递归=(数组,字)=>{
//传递的单词为空,因此我们可以生成当前单词
//还有其他单词的列表
如果(字==''){
返回true;
}
//查看其他单词列表中的单词
//你能开始编这个词吗
常量候选者=array.filter(
(其他单词)=>word.indexOf(其他单词)==0,
);
如果(!candidates.length){
console.warn('为单词放弃',单词);
}
返回(
//没有候选人,返回错误
!!长度&&
//递归地尝试每个候选项
减少(
(结果,其他单词)=>
结果| |//尝试每一个结果,直到结果为真
//使用其他单词的完整列表,但删除
//用于此支票的另一个单词来自
//当前单词
console.log(
“尝试使用单词:”,
单词
'使用候选:',
JSON.stringify(otherWord),
) ||
重复(数组,单词.替换(其他单词“”),
错误的
)
);
};
//返回递归函数
复发(
大堆
//不要使用比当前单词长的其他单词
.过滤器((w)=>w.长度w!==字),
单词
);
};
const words=['x','xxs','xxsxxxx'];
常量结果=单词
.map((word)=>[word.length,word])
.sort(([a],[b])=>b-a)
.map(([[uu,word])=>word)
.找到(
(单词、索引、全部)=>
canMakeWord(全部,word),
);
//.map((单词,索引,全部)=>[canMakeWord(全部,单词),单词])
////去掉所有无法用语言拼凑的单词
////换句话说
//.filter(([canMake])=>canMake)
//.地图(
////映射到[wordLength,word]
//([[uu,word])=>[word.length,word],
// )
//.分类(
//([a],[b])=>b-a,//排序到最长单词
// );
log('RESULT:');

控制台日志(结果)
不确定这是否是您想要的,但
findLongestCombination(arr)
返回由数组中的其他单词(每个单词只使用一次)构建的单词的最长组合。在这种情况下:[“123”、“111”、“1”、“3”]

它通过尝试各种可能的构词方法,递归地使用
findLongestCombinationSingle(word,otherWords)
找到剩余单词中构词最长的组合

如果您有问题,或者我不理解这个问题,请随时发表评论

arr = ['123', '111', '12311113', '1', '2', '3'];

console.log(findLongestCombination(arr));

function findLongestCombination(arr){
  var result = [];
  for(var i=0; i<arr.length; i++){
    var arrOthers = arr.slice(0,i).concat(arr.slice(i+1));
    var comb = findLongestCombinationSingle(arr[i], arrOthers);
    if(comb.length > result.length) result = comb;
  }
  return result;
}

function findLongestCombinationSingle(word, otherWords){
    var result = [];
    for(var i=0; i<otherWords.length; i++){
    if(word.startsWith(otherWords[i])){
        var wordsLeft = otherWords.slice(0,i).concat(otherWords.slice(i+1));
      var restWord = word.replace(otherWords[i], "");
      var subresult = [otherWords[i]].concat(findLongestCombinationSingle(restWord, wordsLeft));
      if(subresult.length > result.length) result = subresult;
    }
  }
  return result;
}
arr=['123','111','12311113','1','2','3'];
console.log(findlongest组合(arr));
函数findLongestCombination(arr){
var结果=[];
对于(var i=0;i result.length)result=comb;
}
返回结果;
}
函数findLongTestCombinationSingle(字、其他字){
var结果=[];
对于(var i=0;i result.length)result=subresult;
}
}
返回结果;
}


如果一个单词不可组合,它不会中断…必须解决这个问题,给我一些时间

标记是为了方便网站的搜索功能并定义问题的范围。不要垃圾标记,它只会引起你的不好的注意。@StoryTeller,Thanx,我编辑了它。那么你想从包含大多数单词的数组中获取这个单词吗?有多少个单词你在处理吗?@juvian,单词可以多于1个。不,你可以在任何时候使用任何单词。但是
结果
应该已经存在于数组中。我在家后会修改它。这对小数组很好,但对非常大的数组来说需要太多时间。@rupshyadav你可以先按最长的单词排序单词,然后在f发现第一个单词可能由所有其他单词组成。如果有多个最长的单词(例如最长的是6,并且您有多个6长的单词可以使用数组中的其他单词创建),则您将得到错误的结果(仅其中一个)。我更新了答案以执行此操作。同时删除console.logs,因为它们仅用于显示代码的工作方式(尝试将所有内容与您在评论中所述的内容进行匹配:
您可以随时使用任何单词
)。@rupshyadav您可以使用数组中最长的单词重复此操作。
arr = ['123', '111', '12311113', '1', '2', '3'];

console.log(findLongestCombination(arr));

function findLongestCombination(arr){
  var result = [];
  for(var i=0; i<arr.length; i++){
    var arrOthers = arr.slice(0,i).concat(arr.slice(i+1));
    var comb = findLongestCombinationSingle(arr[i], arrOthers);
    if(comb.length > result.length) result = comb;
  }
  return result;
}

function findLongestCombinationSingle(word, otherWords){
    var result = [];
    for(var i=0; i<otherWords.length; i++){
    if(word.startsWith(otherWords[i])){
        var wordsLeft = otherWords.slice(0,i).concat(otherWords.slice(i+1));
      var restWord = word.replace(otherWords[i], "");
      var subresult = [otherWords[i]].concat(findLongestCombinationSingle(restWord, wordsLeft));
      if(subresult.length > result.length) result = subresult;
    }
  }
  return result;
}