--- id: 5e4ce2b6ac708cc68c1df25e title: しりとり (Last letter-first letter) challengeType: 5 forumTopicId: 385256 dashedName: last-letter-first-letter --- # --description-- 子供たちの遊びのひとつで、あるカテゴリに属する単語の連想から始めます。 各参加者は順番に単語を言いますが、その単語は前の単語の最後の文字で始まる必要があります。 一度誰かが言った単語を繰り返して使うことはできません。 そのカテゴリの単語を言えなかった参加者は、ゲームから脱落します。 例えば、カテゴリが「動物」であるとします。
子供 1: dog
子供 2: goldfish
子供 1: hippopotamus
子供 2: snake
...
# --instructions-- 単語の入力配列を取る関数を記述してください。 関数は、各単語の最初の文字が前の単語の最後の文字と同じである単語の配列を返す必要があります。 入力配列内の単語のみを使用し、一度使用された単語を繰り返して使うことはできません。 戻り値となる配列の単語は、その長さが最大となるよう選択され、並べられる必要があります。 # --hints-- `findLongestChain` は関数とします。 ```js assert(typeof findLongestChain == 'function'); ``` `findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])` は配列を返す必要があります。 ```js assert( Array.isArray( findLongestChain([ 'certain', 'each', 'game', 'involves', 'starting', 'with', 'word' ]) ) ); ``` `findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])` は `["involves", "starting", "game", "each"]` を返す必要があります。 ```js assert.deepEqual( findLongestChain([ 'certain', 'each', 'game', 'involves', 'starting', 'with', 'word' ]), ['involves', 'starting', 'game', 'each'] ); ``` `findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"])` は `["braviary", "yamask", "kangaskhan"]` を返す必要があります。 ```js assert.deepEqual( findLongestChain([ 'audino', 'bagon', 'kangaskhan', 'banette', 'bidoof', 'braviary', 'exeggcute', 'yamask' ]), ['braviary', 'yamask', 'kangaskhan'] ); ``` `findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"])` は `["poliwrath", "harp", "poochyena", "archana"]` を返す必要があります。 ```js assert.deepEqual( findLongestChain([ 'harp', 'poliwrath', 'poochyena', 'porygon2', 'porygonz', 'archana' ]), ['poliwrath', 'harp', 'poochyena', 'archana'] ); ``` `findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"])` は `["scolipede", "elephant", "tigers", "sealeo"]` を返す必要があります。 ```js assert.deepEqual( findLongestChain([ 'scolipede', 'elephant', 'zeaking', 'sealeo', 'silcoon', 'tigers' ]), ['scolipede', 'elephant', 'tigers', 'sealeo'] ); ``` `findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"])` は `["machamp", "petilil", "lumineon", "nosepass"]` を返す必要があります。 ```js assert.deepEqual( findLongestChain([ 'loudred', 'lumineon', 'lunatone', 'machamp', 'magnezone', 'nosepass', 'petilil', 'pidgeotto', 'pikachu' ]), ['machamp', 'petilil', 'lumineon', 'nosepass'] ); ``` # --seed-- ## --seed-contents-- ```js function findLongestChain(items) { } ``` # --solutions-- ```js function findLongestChain(items) { function Ref(index, first_char, last_char) { this.index = index; this.first_char = first_char; this.last_char = last_char; } var items_len = items.length var refs_len = items_len; var refs = [] // enough space for all items var longest_path_refs_len = 0; var longest_path_refs = new Array(items_len); function search(curr_len) { if (curr_len > longest_path_refs_len) { longest_path_refs_len = curr_len; for (var i = 0; i < curr_len; i++) { longest_path_refs[i] = refs[i]; } } // recursive search var last_char = refs[curr_len - 1].last_char; for (var i = curr_len; i < refs_len; i++) if (refs[i].first_char == last_char) { var aux = refs[curr_len]; refs[curr_len] = refs[i]; refs[i] = aux; search(curr_len + 1); refs[i] = refs[curr_len]; refs[curr_len] = aux; } } for (var i = 0; i < items_len; i++) { var itemsi_len = items[i].length; refs.push(new Ref(i, items[i][0], items[i][itemsi_len - 1])); } // try each item as possible start for (var i = 0; i < items_len; i++) { var aux = refs[0]; refs[0] = refs[i]; refs[i] = aux; search(1); refs[i] = refs[0]; refs[0] = aux; } var longest_path_len = longest_path_refs_len; var longest_path = new Array(longest_path_len); for (var i = 0; i < longest_path_len; i++) longest_path[i] = items[longest_path_refs[i].index]; return longest_path; } ```