Factorize string into Lyndon words: Difference between revisions

Add MATLAB implementation
(Add Rust implementation)
(Add MATLAB implementation)
Line 37:
}
</syntaxhighlight>
 
 
=={{header|MATLAB}}==
{{trans|Python}}
<syntaxhighlight lang="MATLAB">
clear all;close all;clc;
m = '0';
for i = 1:7
m0 = m;
m = strrep(m, '0', 'a');
m = strrep(m, '1', '0');
m = strrep(m, 'a', '1');
m = strcat(m0, m);
end
 
factorization = chenFoxLyndonFactorization(m);
disp(factorization);
 
 
function factorization = chenFoxLyndonFactorization(s)
n = length(s);
i = 1;
factorization = {};
while i <= n
j = i + 1;
k = i;
while j <= n && s(k) <= s(j)
if s(k) < s(j)
k = i;
else
k = k + 1;
end
j = j + 1;
end
while i <= k
factorization{end+1} = s(i:i + j - k - 1);
i = i + j - k;
end
end
assert(strcmp(join(factorization, ''), s));
end
</syntaxhighlight>
{{out}}
<pre>
Columns 1 through 8
 
{'011'} {'01'} {'0011'} {'00101101'} {'0010110011010011'} {'001011001101001…'} {'001011001101001…'} {'001011001101'}
 
Column 9
 
{'001'}
</pre>
 
== [[Python]] ==
338

edits