// Deletes the elements S[n]...S[k] from the string Delete := procedure(~S,n,k) if k ne #S then S := Substring(S,1,n-1)*Substring(S,k+1,#S); //Note, Substring function deals with overflow caused by #s else S := Substring(S,1,n-1); end if; end procedure; // Capitol letters represent inverses // pq := a, qr := b, rp := c, qp := A, rq := B, pr := C PQRtoABC := procedure(~S) ABC := ""; for k in [1..(#S-1) by 2] do if S[k] eq "p" then if S[k+1] eq "q" then ABC := ABC*"a"; else ABC := ABC*"C"; end if; elif S[k] eq "q" then if S[k+1] eq "r" then ABC := ABC*"b"; else ABC := ABC*"A"; end if; else if S[k+1] eq "p" then ABC := ABC*"c"; else ABC := ABC*"B"; end if; end if; end for; if #S mod 2 eq 1 then ABC := ABC*S[#S]; end if; S := ABC; end procedure; //Destructively changes an ABCabc(pqr) string to a pqr string ABCtoPQR := procedure(~S) PQR := ""; for k in [1..#S] do if S[k] eq "a" then PQR := PQR*"pq"; elif S[k] eq "b" then PQR := PQR*"qr"; elif S[k] eq "c" then PQR := PQR*"rp"; elif S[k] eq "A" then PQR := PQR*"qp"; elif S[k] eq "B" then PQR := PQR*"rq"; elif S[k] eq "C" then PQR := PQR*"pr"; else PQR := PQR*S[k]; end if; end for; S := PQR; end procedure; // removes l, m, and n powers of a, b, and c, respectively Cycling := procedure(~S,l,m,n) location := Position(S,"a"^l); while location ne 0 do Delete(~S,location,location+l-1); location:= Position(S,"a"^l); end while; location:= Position(S,"A"^l); while location ne 0 do Delete(~S,location,location+l-1); location:= Position(S,"A"^l); end while; location:= Position(S,"b"^m); while location ne 0 do Delete(~S,location,location+m-1); location:= Position(S,"b"^m); end while; location:= Position(S,"B"^m); while location ne 0 do Delete(~S,location,location+m-1); location:= Position(S,"B"^m); end while; location:= Position(S,"c"^n); while location ne 0 do Delete(~S,location,location+n-1); location:= Position(S,"c"^n); end while; location:= Position(S,"C"^n); while location ne 0 do Delete(~S,location,location+n-1); location:= Position(S,"C"^n); end while; end procedure; Samplify := procedure(~S,l,m,n) Size := -1; while Size ne #S do Size := #S; k := 1; while k lt #S do if S[k] eq S[k+1] then Delete(~S,k,k+1); else k := k + 1; end if; end while; PQRtoABC(~S); Cycling(~S,l,m,n); ABCtoPQR(~S); end while; end procedure; Samp := function(S,l,m,n); Samplify(~S,l,m,n); return S; end function; //Destructively simplifies a string Simplify := procedure(~S,l,m,n) Size := -1; while Size ne #S do Size := #S; k := 1; while k lt #S do if S[k] eq S[k+1] then Delete(~S,k,k+1); else k := k + 1; end if; end while; end while; Size := -1; while Size ne #S do Size := #S; PQRtoABC(~S); Cycling(~S,l,m,n); ABCtoPQR(~S); end while; Size := -1; while Size ne #S do Size := #S; k := 1; while k lt #S do if S[k] eq S[k+1] then Delete(~S,k,k+1); else k := k + 1; end if; end while; PQRtoABC(~S); Cycling(~S,l,m,n); ABCtoPQR(~S); end while; if #S mod 2 eq 1 then P := Samp(Samp(S*"p",l,m,n)*"p",l,m,n); Q := Samp(Samp(S*"q",l,m,n)*"q",l,m,n); R := Samp(Samp(S*"r",l,m,n)*"r",l,m,n); S := P; if #S gt #Q then S := Q; end if; if #S gt #R then S := R; end if; end if; end procedure; // Version of simplify that doesnt' require a variable reference and doesn't change the value of the variable SimpLite := function(S,l,m,n) Simplify(~S,l,m,n); return S; end function; SimpMax := function(S,l,m,n) Simplify(~S,l,m,n); Size := 0; while Size ne #S do Size := #S; result := S; for k in [1..(#S-1)] do for j in [k..(#S-1)] do tmp := SimpLite(Substring(S,1,k),2,3,7)*SimpLite(Substring(S,k+1,j),2,3,7)*SimpLite(Substring(S,j+1,#S),2,3,7); if #tmp lt #result then result := tmp; end if; end for; end for; S := result; end while; return S; end function;