对自已的算法不是很满意.不过能解决问题.
procedure StrTaxis(str: string; var retStr: TStrings);
{
str: 组合的元素,如'abcd'
retStr: 组合的结果集
}
var
i,j,i1,j1,l: Integer;
s,s1,s2: String;
begin
retStr.Clear;
for i := 1 to length(str) do
begin
for j := 1 to length(str)-i+1 do
begin
s1 := copy(str,j,i);
s2 := s1;
repeat
for i1 := 1 to length(s2) do
begin
s := copy(s2,i1,1);
for j1 := 1 to length(s2) do
begin
if (i1<>j1) then
s := s + copy(s2,j1,1);
end;
if retStr.IndexOf(s)<0 then retStr.Add(s);
end;
l := length(s1);
if l>1 then
s2:=copy(s2,l,1)+copy(s2,1,l-1)
else
until (s1=s2);
end;
end
end;
使用例子:
procedure TForm1.Button1Click(Sender: TObject);
var
aaa:TStrings;
begin
aaa := TStringList.Create;
StrTaxis(edit1.text,aaa);
memo1.Clear;
memo1.Lines.AddStrings(aaa);
end;