排列组合

///////代码开始
function PermutationCombination(mArr: array of string;
  mStrings: TStrings): Boolean;
var
  I, J: Integer;
  T: string;
  S: string;
begin
  Result := False;
  if not Assigned(mStrings) then Exit;
  mStrings.Clear;
  T := '';
  for I := Low(mArr) to High(mArr) do
    if mArr[I] <> '' then begin
      T := T + mArr[I][1];
      S := S + mArr[I][Length(mArr[I])];
    end else Exit;
  while T <> S do try
    mStrings.Add(T);
    J := Length(S);
    for I := High(mArr) downto Low(mArr) do begin
      if Pos(T[J], mArr[I]) >= Length(mArr[I]) then
        T[J] := mArr[I][1]
      else begin
        T[J] := mArr[I][Pos(T[J], mArr[I]) + 1];
        Break;
      end;
      Dec(J);
    end;
    mStrings.Add(S);
  except
    Exit;
  end;
  Result := True;
end; { PermutationCombination }
///////代码结束

///////示例开始
procedure TForm1.Button1Click(Sender: TObject);
begin
  PermutationCombination(['ACDE', 'FGH', 'IJKL', 'MNO', 'PQRST'], Memo1.Lines)
end;
///////示例结束

你可能感兴趣的:(组合)