全排列,比如字母ABC,所有排列有A ,AB,AC,ABC,ACB,B,BA,BC,BAC,BCA,C,CA,CB,CAB,CBA。
在我的机器上排列10个字母大约0.5秒。
//cczlp
//原理是插入, 在一个字符串的所有位置插入新字符.
//如: AB 插入C , 位置有 1A2B3, 插入后形成 CAB ACB ABC
void AllList(TStringList *lst, AnsiString str)//原理, 速度慢
{
int i, j, k;
int len;
int count;
AnsiString as;
for (k = 1; k <= str.Length(); k++) //所有要插入的字符
{
count = lst->Count;
for (i = 0; i < count; i++) //插入到所有字符串中,形成新的字符串
{
len = lst->Strings[i].Length();
for (j = 1; j <= len + 1; j++) //在字符串所有位置插入
{
as = lst->Strings[i];
lst->Add(as.Insert(str[k], j));
}
}
lst->Add(str[k]);
}
}
char *AllList(char *str, int *pNum)//速度快
{
int i, j, k, n;
int len = strlen(str);
int Total = 0;
int count, oldcount;
int size;
char *Buf;
char *p, *p1;
if (len > 10) return NULL;
//计算总的组合数目
for (i = 0, j = 1; i < len; i++)
{
j *= (len - i);
Total += j;
}
//创建二维数组, 存放全部组合
size = len + 1;
if ((Buf = (char *)malloc(Total * size)) == NULL)
{
return NULL;
}
for (k = 0, count = 0; k < len; k++) //所有要插入的字符
{
oldcount = count;
p = Buf;
p1 = Buf + count * size;
for (i = 0; i < oldcount; i++, p += size) //插入到所有字符串中,形成新的字符串
{
n = strlen(p);
for (j = 0; j <= n; j++, count++, p1 += size) //在字符串所有位置插入
{
memcpy(p1, p, n);
p1[n] = p1[j];
p1[j] = str[k];
p1[n + 1] = '\0';
}
}
Buf[count * size] = str[k];
Buf[count++ * size + 1] = '\0';
}
*pNum = count; //排列总数
return Buf;
}
DWORD __fastcall Getms(void)
{
static __int64 freq = 0;
LARGE_INTEGER t1;
if (freq == 0)
{
QueryPerformanceFrequency(&t1);
freq = t1.QuadPart;
}
QueryPerformanceCounter(&t1);
return t1.QuadPart * 1000.0 / freq;
}
void __fastcall TForm1::Button5Click(TObject *Sender)
{
DWORD t1, t2; //4567890
char *str = "ABCDEFGHIJ";
TStringList *lst = new TStringList;
t1 = Getms();
AllList(lst, str);
t2 = Getms();
// Memo1->Text = lst->Text;
delete lst;
Memo1->Lines->Add(IntToStr(t2 - t1));
int num;
char *Buf;
int len = strlen(str);
t1 = Getms();
Buf = AllList(str, &num);
t2 = Getms();
Memo2->Lines->Add(IntToStr(t2 - t1));
free(Buf);
}