% https://open.kattis.com/problems/redrover % 1s % 1.9 Easy % Ah, Picat does not have a string version of replace. % So we have to use a function for that... % It's shorter 466 chars (vs 846 chars) % but still too long for my taste... import util. main :- S = read_file_lines().first, Len = S.len, Subs = [S[I..J] : I in 1..Len,J in I+1..Len,J-I < Len div 2].remove_dups, s(Subs,S,10000,Min), writeln(min(Min,Len)). s([],_,M,M). s([Sub|Subs],S,M0,M) :- S2 = replace2(S,Sub,"m"), Len = Sub.len + S2.len, s(Subs,S,min(Len,M0),M). replace2(List,Old,New) = Res => Res = List, while (find(Res,Old,_,_)) once(append(Before,Old,After,Res)), Res := Before ++ New ++ After end.