Superpermutation minimisation: Difference between revisions

m
→‎version 2: changed whitespace.
m (→‎version 1: aligned statement, changed whitespace.)
m (→‎version 2: changed whitespace.)
Line 1,804:
do n=0 to cycles
#= 0; $.= /*populate the first permutation. */
do pop=1 for n; @.pop= d2x(pop); $.0= $.0 || @.pop
end /*pop*/
 
do while aPerm(n,0); if n\==0 then #= #+1; $.#=
do j=1 for n; $.#= $.# || @.j
end /*j*/
end /*while*/
z= $.0
c= 0 c= 0 /*count of found permutations (so far).*/
do j=1 while c\==#
if j># then do; c= c + 1 /*exhausted finds and shortcuts; concat*/
Line 1,819:
end
if $.j=='' then iterate /*Already found? Then ignore this perm.*/
if pos($.j, z)\==0 then do; c= c + 1; $.j=
$.j=
iterate
end
 
do k=n-1 to 1 by -1 /*handle the shortcuts in perm finding.*/
if substr($.j, k)==left(z, k) then do; c= c+1 /*found a rightish shortcut*/
z= left($.j, k-1) || z; $.j=
iterate j
end
if left($.j, k) ==right(z, k) then do; c= c+1 /*found a leftish shortcut*/
z= z || substr($.j, k+1); $.j=
iterate j
Line 1,835 ⟶ 1,834:
end /*k*/ /* [↑] more IFs could be added for opt*/
end /*j*/
 
L= commas( length(z) )
say 'length of superpermutation('n") =" right(L, max(length(L), cycles+2) )