A certain children's game involves starting with a word in a particular category.   Each participant in turn says a word, but that word must begin with the final letter of the previous word.   Once a word has been given, it cannot be repeated.   If an opponent cannot give a word in the category, they fall out of the game.



For example, with   "animals"   as the category,

Child 1: dog 
Child 2: goldfish
Child 1: hippopotamus
Child 2: snake
...



Task

Take the following selection of 70 English Pokemon names   (extracted from   Wikipedia's list of Pokemon)   and generate the/a sequence with the highest possible number of Pokemon names where the subsequent name starts with the final letter of the preceding name.

No Pokemon name is to be repeated.

audino bagon baltoy banette bidoof braviary bronzor carracosta charmeleon
cresselia croagunk darmanitan deino emboar emolga exeggcute gabite
girafarig gulpin haxorus heatmor heatran ivysaur jellicent jumpluff kangaskhan
kricketune landorus ledyba loudred lumineon lunatone machamp magnezone mamoswine
nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena porygon2
porygonz registeel relicanth remoraid rufflet sableye scolipede scrafty seaking
sealeo silcoon simisear snivy snorlax spoink starly tirtouga trapinch treecko
tyrogue vigoroth vulpix wailord wartortle whismur wingull yamask



Extra brownie points for dealing with the full list of   646   names.




with javascript_semantics
constant words = {"audino","bagon","baltoy","banette","bidoof","braviary","bronzor","carracosta","charmeleon","cresselia","croagunk",
                  "darmanitan","deino","emboar","emolga","exeggcute","gabite","girafarig","gulpin","haxorus","heatmor","heatran",
                  "ivysaur","jellicent","jumpluff","kangaskhan","kricketune","landorus","ledyba","loudred","lumineon","lunatone",
                  "machamp","magnezone","mamoswine","nosepass","petilil","pidgeotto","pikachu","pinsir","poliwrath","poochyena",
                  "porygon2","porygonz","registeel","relicanth","remoraid","rufflet","sableye","scolipede","scrafty","seaking",
                  "sealeo","silcoon","simisear","snivy","snorlax","spoink","starly","tirtouga","trapinch","treecko","tyrogue",
                  "vigoroth","vulpix","wailord","wartortle","whismur","wingull","yamask"}
 
function word_chains()
    sequence first = repeat(0,256),             -- start of chains for a given letter
                                                -- first['a']=1, first['b']=2, first['c']=8, etc.
             snext = repeat(0,length(words))    -- chains of words starting with the same letter
                                                -- a: snext[1]=0, b: snext[2..7]={3,4,5,6,7,0}, etc.
    for i=1 to length(words) do
        integer ch = words[i][1]
        if first[ch]=0 then
            first[ch] = i
        end if
        for j=i+1 to length(words) do
            if words[j][1]=ch then
                snext[i] = j
                exit
            end if
        end for
    end for
    return {first,snext}
end function
 
constant {first,snext} = word_chains()
 
-- maintain words already taken as a linked list:
integer tstart
sequence taken = repeat(0,length(words))    -- 0=no, -1=end of chain, +ve=next
 
-- and keep a copy of the best for later
integer bstart
sequence best
integer maxn = 0, count
 
procedure find_path(integer ch, last, n)
    integer next = first[ch]
    while next!=0 do
        if taken[next]=0 then
            taken[last] = next
            taken[next] = -1
            find_path(words[next][$],next,n+1)
            taken[last] = -1
            taken[next] = 0
        end if
        next = snext[next]
    end while
    if n>maxn then
        bstart = tstart
        best = deep_copy(taken)
        maxn = n
        count = 1
    elsif n=maxn then
        count += 1
    end if
end procedure
 
atom t0=time()
 
for i=1 to length(words) do
    tstart = i
    taken[i] = -1
    find_path(words[i][$],i,1)
    taken[i] = 0
end for
 
printf(1,"Runtime: %2.3f seconds. Max length:%d, found %d of such, one of which is:\n",{time()-t0,maxn,count})
sequence res = {}
while bstart!=-1 do
    res = append(res,words[bstart])
    bstart = best[bstart]
end while
assert(length(res)=maxn)
printf(1,"%s\n",join_by(res,1,10," "))
