Find this algorithm

Source: Internet
Author: User
For example, I have an array.
$array=Array("a",'b','c','d','e','f','g');

Of course there are more

The output result is as follows:

A
B
C
D
E
F
G
Aa
AB
Ac
Ad
AE
Af
Ag
Ba
Bb
Bc
Bd
Be
Bf
Bg
Ca
Cb
Cc

............

Gaaaaaaa
Gaaaaaab
Gaaaaaac
Gaaaaaad
Gaaaaaae

............

Is to list all the strings that can contain these characters.


Reply to discussion (solution)

The LZ question seems a bit ambiguous, such as the letter.
A calculates an aa and an aaa, and an aaaaaaaaaaaaaaaaaaaaaaaaaaaa.
How much is the end?

For example, I have an array

$array=Array("a",'b','c','d','e','f','g');

Of course there are more

The output result is as follows:

A
B
C
D
E
F
G
Aa
AB
Ac
Ad
AE
Af
Ag
Ba
Bb
Bc
Bd
Be
Bf
Bg
Ca
Cb
Cc

............

Gaaaaaaa
Gaaaaaab
Gaaaaaac
Gaaaaaad
Gaaaaaae

............

Is to list all the strings that can contain these characters.

No problem. it's just an infinite number...

$ Array = Array ("a", 'B', 'C', 'D', 'e', 'F', 'g ');
For ($ I =-1; $ I
Foreach ($ array as $ k => $ v ){
Echo $ array [$ I]. $ v ."
";
}
}
Hope to help you ~

When will your program stop ....

No problem. it's just an infinite number...

When will your program stop ....



No problem. it's just an infinite number...

Unplug the power...

It is estimated that it is intended to be a collision machine.


When will your program stop ....



No problem. it's just an infinite number...

Unplug the power...

Although the requirements of the landlord seem to be somewhat unappealing, it is not impossible to set the termination conditions.
With a limited number, this algorithm is still very challenging.
Let me introduce myself

$ Ar = array ("a", 'B', 'C'); print_r (allot ($ ar, 4); print_r (allot ($ ar, 2 )); function allot ($ ar, $ num) {static $ st = array (); // used to cache intermediate results $ res = array (); // used to hold returned results if (! $ St) {$ t = $ ar;} else {$ t = array (); foreach ($ ar as $ v) {$ t = array_merge ($ t, array_map (function ($ item, $ prefix) {return $ prefix. $ item ;}, $ st, array_fill (0, count ($ st), $ v) ;}// foreach ($ t as $ v) echo $ v. PHP_EOL; // directly generate the output $ res = $ st = $ t; if ($ num> 1) $ res = array_merge ($ res, allot ($ ar, $ num-1); else $ st = array (); // initial cache so that the next call to return $ res ;}
Array
(
[0] =>
[1] => B
[2] => c
[3] => aa
[4] => AB
[5] => ac
[6] => ba
[7] => bb
[8] => bc
[9] => ca
[10] => cb
[11] => cc
[12] => aaa
[13] => aab
[14] => aac
[15] => aba
[16] => abb
[17] => abc
[18] => aca
[19] => acb
[20] => acc
[21] => baa
[22] => bab
[23] => bac
[24] => bba
[25] => bbb
[26] => bbc
[27] => bca
[28] => bcb
[29] => bcc
[30] => caa
[31] => cab
[32] => cac
[33] => CBA
[34] => cbb
[35] => cbc
[36] => cca
[37] => ccb
[38] => ccc
[39] => aaaa
[40] => aaab
[41] => aaac
[42] => aaba
[43] => aabb
[44] => aabc
[45] => aaca
[46] => aacb
[47] => aacc
[48] => abaa
[49] => abab
[50] => abac
[51] => abba
[52] => abbb
[53] => abbc
[54] => abca
[55] => abcb
[56] => abcc
[57] => acaa
[58] => acab
[59] => acac
[60] => acba
[61] => acbb
[62] => acbc
[63] => acca
[64] => accb
[65] => accc
[66] => baaa
[67] => baab
[68] => baac
[69] => baba
[70] => babb
[71] => babc
[72] => baca
[73] => bacb
[74] => bacc
[75] => bbaa
[76] => bbab
[77] => bbac
[78] => bbba
[79] => bbbb
[80] => bbbc
[81] => bbca
[82] => bbcb
[83] => bbcc
[84] => bcaa
[85] => bcab
[86] => bcac
[87] => bcba
[88] => bcbb
[89] => bcbc
[90] => bcca
[91] => bccb
[92] => bccc
[93] => caaa
[94] => caab
[95] => caac
[96] => caba
[97] => cabb
[98] => cabc
[99] => caca
[100] => cacb
[101] => cacc
[2, 102] => cbaa
[2, 103] => cbab
[104] => cbac
[1, 105] => cbba
[106] => cbbb
[107] => cbbc
[108] => cbca
[109] => cbcb
[2, 110] => CB
[111] => ccaa
[112] => ccab
[113] => ccac
[2, 114] => ccba
[1, 115] => ccbb
[2, 116] => ccbc
[117] => ccca
[118] => cccb
[2, 119] => cccc
)
Array
(
[0] =>
[1] => B
[2] => c
[3] => aa
[4] => AB
[5] => ac
[6] => ba
[7] => bb
[8] => bc
[9] => ca
[10] => cb
[11] => cc
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.