Finish 2000-03-27 00:00:00 UTC

GFD4.2n_rand_flip

by Edward Brian Welch

Status: Passed
Results: 99.9942
CPU Time: 120.222
Score: 51790.2
Submitted at: 2000-03-24 04:09:44 UTC
Scored at: 2000-03-24 04:16:37 UTC

Current Rank: 145th
Based on: GFD4.2n_rand3 (diff)

Comments
Edward Brian Welch
24 Mar 2000
Try flipping left-right for a new perspective
Please login or create a profile.
Code
function Sequence = GFD4_rand_flip(Segments)

[nSegs, lSegs] = size(Segments);

iter=10;
RandomNumbers = rand(lSegs,iter);
BestLength = nSegs*lSegs;
for x=1:iter,
  
  if mod(x,2)==0,
    newSequence = fliplr(getSequence(fliplr(Segments),RandomNumbers(:,x)));
  else
    newSequence = getSequence(Segments,RandomNumbers(:,x));
  end

  if length(newSequence)<BestLength,
    Sequence = newSequence;
    BestLength = length(Sequence);
  end

end

function Sequence = getSequence(Segments,RandomNumbers)

[nSegs, lSegs] = size(Segments);
[SortedHash Order]  = sort(Segments * RandomNumbers);
Segments = Segments(Order(diff([SortedHash ; 0]) ~= 0), :);
[nSegs, lSegs] = size(Segments);

theSegments = cell(nSegs, 1);
theSegs = 1:nSegs;
for index = theSegs,
   theSegments{index} = Segments(index, :);
end
copySegments = theSegments;

EndPart = 2-lSegs:0;
for matchLength = lSegs-1:-1:1
   Segments(:, 1) = [];
   for leftSeg = theSegs
      Match = find(strncmp(Segments(leftSeg, :), copySegments, matchLength));
      if Match
         Select = Match(1);
         rightSeg = theSegs(Select);
         if rightSeg == leftSeg 
            if length(Match) > 1 % Rare case
               temp = Match(Match ~= Select);
               Select = temp(1);
               rightSeg = theSegs(Select); 
               
               theSegments{rightSeg} = [theSegments{leftSeg} theSegments{rightSeg}(1+matchLength:end)];
               i = find(theSegs == leftSeg);
               theSegs(i) = [];
               if length(theSegs) == 1 
                  Sequence = [theSegments{theSegs}];
			   return;					
               end
               copySegments(Select) = copySegments(i);
               copySegments(i) = [];
               end
         else
            theSegments{rightSeg} = [theSegments{leftSeg} theSegments{rightSeg}(1+matchLength:end)];
            i = find(theSegs == leftSeg);
            theSegs(i) = [];
            if length(theSegs) == 1 
               Sequence = [theSegments{theSegs}];
               return;
            end
            copySegments(Select) = copySegments(i);
            copySegments(i) = [];
         end
      end
   end
   EndPart(1) = [];   
end
Sequence = [theSegments{theSegs}];