47 lines
1.4 KiB
Plaintext
47 lines
1.4 KiB
Plaintext
go =>
|
|
Strings = [
|
|
"1001110011", % 10011
|
|
"1110111011", % 1110
|
|
"0010010010", % 001
|
|
"1010101010", % 1010
|
|
"1111111111", % 11111
|
|
"0100101101", % no solution
|
|
"0100100", % 010
|
|
"101", % no solution
|
|
"11", % 1
|
|
"00", % 0
|
|
"1", % no solution
|
|
"", % no solution
|
|
"123123123123123", % 123123
|
|
"12312312312312", % 123123
|
|
"123123123123124", % no solution
|
|
"abcabcdabcabcdabc", % abcabcd
|
|
[1,2,3,4,1,2,3,4,1,2,3] % 1,2,3,4
|
|
|
|
],
|
|
foreach(S in Strings)
|
|
printf("%w: ", S),
|
|
if maxrep(S,Substr,N) then
|
|
println([substr=Substr,n=N])
|
|
else
|
|
println("no solution")
|
|
end
|
|
end,
|
|
nl.
|
|
|
|
% the largest repeating substring
|
|
maxrep(S,Substr,N) =>
|
|
maxof(rep(S,Substr,N),N).
|
|
|
|
rep(S,Substr,N) =>
|
|
between(1,S.length div 2, N),
|
|
Len = S.length,
|
|
Len2 = Len - (Len mod N),
|
|
Substr = slice(S,1,N),
|
|
% count the number of proper slices
|
|
SS = [1 : I in 1..N..Len2, slice(S,I,I+N-1) = Substr],
|
|
SS.length = Len div N,
|
|
% the last (truncated) slice (or []) must be a substring of Substr
|
|
Rest = slice(S,Len2+1,Len),
|
|
find(Substr,Rest,1,_).
|