Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write C code for fast algorithm of full arrangement of letters

Shulou Source: shulou.com Published: 2022-06-03 05:39:36 10月01日 Update

This article introduces how to write a fast algorithm C code for full arrangement of letters. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

All permutations, such as the letter ABC, all permutations have A, AB,AC,ABC,ACB,B,BA,BC,BAC,BCA,C,CA,CB,CAB,CBA.

/ / the principle is to insert, insert new characters at all positions in a string.

/ / for example, AB is inserted into C, where there is 1A2B3, and CAB ACB ABC is formed after insertion.

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

/ / calculate the total number of combinations

For (I = 0, j = 1; I len; iTunes +)

... {

J * = (len-I)

Total + = j

}

/ / create a two-dimensional array to store all combinations

Size = len + 1

If ((Buf = (char *) malloc (Total * size)) = = NULL)

... {

Return NULL

}

For (k = 0, count = 0; k len; kits +) / / all characters to be inserted

... {

Oldcount = count

P = Buf

P1 = Buf + count * size

For (I = 0; I oldcount; iTunes, p + = size) / / is inserted into all strings to form a new string

... {

N = strlen (p)

For (j = 0; j n; jblocks, count++, p1 + = size) / / insert at all positions of the string

... {

Memcpy (p1, p, n)

P1 [n] = p1 [j]

P1 [j] = str [k]

P1 [n + 1] ='

Arrange 10 letters on my machine for about 0.5 seconds.

On how to write the full arrangement of letters fast algorithm C code is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Characters letters strings positions codes algorithms content more help combinations good interests principles guys buddies numbers arrays articles new words machines Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Shulou Tech Info Docker Linux