Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report