Advertisement
TungstenVn

Untitled

Oct 29th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var outtemp = ""; var outtemp2 = [];
  2. //longestWordChain(["abc","cba","ccc","cab","abca","acd","dabc","adc","c","ca"], "c");
  3. function longestWordChain(keywords, startWord){
  4.     var check = false;
  5.     var chuoihientai = startWord;
  6.     for(var i = 0;i < keywords.length;i++){
  7.         if(startWord[startWord.length -1] == keywords[i][0]  && chuoihientai.split("-").indexOf(keywords[i]) == -1){        
  8.             dequytimchu(chuoihientai + "-" + keywords[i],keywords[i],keywords);        
  9.             check = true;
  10.         }
  11.     }
  12.     if(!check){
  13.         return startWord;
  14.     }
  15.     if(outtemp2.length == 1){
  16.       return outtemp2[0];
  17.     }
  18.     var outtemp3 = [];var checkdodai = 0;
  19.     for(var i = 0;i<outtemp2.length;i++){
  20.       if(outtemp2[i].split("-").length >= checkdodai){
  21.         checkdodai = outtemp2[i].split("-").length;
  22.       }
  23.     }
  24.      for(var i = 0;i<outtemp2.length;i++){
  25.       if(outtemp2[i].split("-").length == checkdodai){
  26.         outtemp3.push(outtemp2[i]);
  27.       }
  28.     }
  29.     outtemp3.sort();  
  30.     //console.log(outtemp3);
  31.     return outtemp3[0];
  32. }
  33. function dequytimchu(chuoihientai,tutieptheo,keywords){
  34.     var check = false;
  35.     for(var i = 0;i < keywords.length;i++){
  36.        
  37.         if(tutieptheo[tutieptheo.length -1] == keywords[i][0] && keywords[i] != tutieptheo && chuoihientai.split("-").indexOf(keywords[i]) == -1){  
  38.             dequytimchu(chuoihientai +"-"+keywords[i],keywords[i],keywords);        
  39.         }
  40.     }  
  41.     if(outtemp.length <= chuoihientai.length){
  42.             outtemp = chuoihientai;
  43.             outtemp2.push(chuoihientai);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement