High score? Algorithm, online waiting? Upper ?? This post was last edited by sibang at 23:44:40
PHP algorithm function string combination
Please help write a function to re-combine the string, which is roughly as follows:
/** Parameter: $ arr: Original Array $ len: length after combination/**/Function getArray ($ arr, $ len) {// Please write} $ arr = Array ('A', 'B', 'C'); print_r (getArray ($ arr, 2 )); $ arr = Array ('A', 'B', 'C', 'D', 'e', 'F'); print_r (getArray ($ arr, 4 ));
The requirements are as follows:
1st types: A, B, C
The expected combination is AB, AC, and BC.
2nd types: A, B, C, D (the result length can be controlled by parameters, for example, the length is 2 or 3)
The expected combination is AB, AC, AD, BC, BD, CD or ABC, BCD, ACD, and ABD.
3rd types: A, B, C, D, E (the result length can be controlled by parameters, for example, the length is 2; 3 or 4)
Expected combination: AB, AC, AD, AE, BC, BD, BE, CD, CE, DE or ABC, BCD, CDE, ABD, ABE, ACD, ACE, BDE, BCE, ADE or ABCD, BCDE, ABCE, ACDE, ABDE
Type 4: A, B, C, D, E, F (the result length can be controlled by parameters, for example, the length is 2; 3; 4 or 5)
The expected combination is: it can be deduced based on the above combination.
Reply to discussion (solution)
What are the combinations in the permutation and combination?
It looks very interesting... although it may not be possible... try it.
Looking forward to the bright future of algorithm experts?
Sorry... I can't figure it out... I thought it might be better to use deep-first search, but I haven't thought about how to write it ....
A little progress has been made, not to mention algorithms, and the efficiency is very low! I just want to help you get started...
Idea: first calculate the arrangement, and then remove the duplicates in the arrangement to form a combination.
P.S ..
/* $ Arr = array ("A", "B", "C"); Function getArray ($ arr, $ len = 2) {$ arr2 = array (); foreach ($ arr as $ y) {foreach ($ arr as $ k) {if ($ y! = $ K) {$ arr2 [] = $ y. $ k ;}}$ arr3 = array (); foreach ($ arr2 as $ c) {$ f = str_split ($ c); if ($ arr3) {$ I = 0; foreach ($ arr3 as $ d) {$ e = str_split ($ d); if (in_array ($ f [0], $ e) & in_array ($ f [1], $ e) {$ I ++ ;}} if (! $ I) {$ arr3 [] = $ c ;}} else {$ arr3 [] = $ c ;}} return $ arr3 ;} */$ arr = array ("A", "B", "C", "D"); Function getArray ($ arr, $ len = 3) {$ arr2 = array (); foreach ($ arr as $ y) {// The number of len statements is several times, here is a variable foreach ($ arr as $ k) {foreach ($ arr as $ v) {if ($ y! = $ K & $ y! = $ V & $ v! = $ K) {// $ len elements are not the same $ arr2 [] = $ y. $ k. $ v ;}}$ arr3 = array (); foreach ($ arr2 as $ c) {$ f = str_split ($ c); if ($ arr3) {$ I = 0; foreach ($ arr3 as $ d) {$ e = str_split ($ d); if (in_array ($ f [0], $ e) & in_array ($ f [1], $ e) & in_array ($ f [2], $ e) {// $ len if all elements are in the array, $ I ++ if $ I is greater than 0, this record will discard $ I ++;} if (! $ I) {$ arr3 [] = $ c ;}} else {$ arr3 [] = $ c ;}return $ arr3 ;} print_r (getArray ($ arr ));
There are three changes according to $ len, and the four characters of ABCD or five characters of ABCDE can be correctly obtained.
So the key is how to judge and recursion in $ len...
====================================
I always think that this method is a poor method, and I want to criticize and correct it.
A B C D E F G seven-character array, which is arranged in three
The above method is used to conclude that
Array ([0] => ABC [1] => ABD [2] => ABE [3] => ABF [4] => ABG [5] => ACD [6] => ACE [7] => ACF [8] => ACG [9] => ADE [10] => ADF [11] => ADG [12] => AEF [13] => AEG [14] => AFG [15] => BCD [16] => BCE [17] => BCF [18] => BCG [19] => BDE [20] => BDF [21] => BDG [22] => BEF [23] => BEG [24] => BFG [25] => CDE [26] => CDF [27] => CDG [28] => CEF [29] => CEG [30] => CFG [31] => DEF [32] => DEG [33] => DFG [34] => EFG)
35 should be correct .....
There are many methods for "combination ".
First, I will give you a "efficient 10-move method". The function naming should be based on the combined English word combination.
function combination( $arr, $num=0) { $len = count($arr); if($num == 0) $num = $len; $res = array(); for($i=1,$n=pow(2, $len);$i<$n;++$i) { $tmp = str_pad(base_convert($i, 10, 2), $len, '0', STR_PAD_LEFT); $t = array(); for($j=0;$j<$len;++$j) { if($tmp{$j} == '1') { $t[] = $arr[$j]; } } if(count($t) == $num) $res[] = join('', $t); } return $res;}
Call:
$arr = array('A', 'B', 'C');print_r(combination($arr, 2));Array
(
[0] => BC
[1] => AC
[2] => AB
)
Please help write a function to re-combine the string, which is roughly as follows:
/** Parameter: $ arr: Original Array $ len: length after combination/**/Function getArray ($ arr, $ len) {// Please write} $ arr = Array ('A', 'B', 'C'); print_r (getArray ($ arr, 2 )); $ arr = Array ('A', 'B', 'C', 'D', 'e', 'F'); print_r (getArray ($ arr, 4 ));
The requirements are as follows:
1st types: A, B, C
The expected combination is AB, AC, and BC.
2nd types: A, B, C, D (the result length can be controlled by parameters, for example, the length is 2 or 3)
The expected combination is AB, AC, AD, BC, BD, CD or ABC, BCD, ACD, and ABD.
3rd types: A, B, C, D, E (the result length can be controlled by parameters, for example, the length is 2; 3 or 4)
Expected combination: AB, AC, AD, AE, BC, BD, BE, CD, CE, DE or ABC, BCD, CDE, ABD, ABE, ACD, ACE, BDE, BCE, ADE or ABCD, BCDE, ABCE, ACDE, ABDE
Type 4: A, B, C, D, E, F (the result length can be controlled by parameters, for example, the length is 2; 3; 4 or 5)
The expected combination is: it can be deduced based on the above combination.
Are there repeated letters?
Is there AB or BA different?
I have to explain it in detail.
Recursive writing
function combination($ar, $k, $m=0, $a=array()) { static $ret = array(); if($m == 0) { $m = count($ar); $ret = array(); } for($i=$m; $i>=$k; $i--) { $a[$k-1] = $ar[$i-1]; if($k > 1) { combination(&$ar, $k-1, $i-1, $a); }else { array_unshift ($ret, array_reverse($a)); } } return $ret;}Stack writing
function combination($ar, $num) { $control = range(0, $num-1); $k = false; $total = count($ar); while($control[0] < $total-($num-1)) { $t = array(); for($i=0; $i<$num; $i++) $t[] = $ar[$control[$i]]; $r[] = $t; for($i=$num-1; $i>=0; $i--) { $control[$i]++; for($j=$i; $j<$num-1; $j++) $control[$j+1] = $control[$j]+1; if($control[$i] < $total-($num-$i-1)) break; } } return $r;}
Sharp version
6 # Why do I use pow (2, $ len) for the number of loops? why can I solve the problem by converting it to a binary number? This should be a mathematical problem... but I don't want to understand it. please dial it...
Sharp version
6 # Why do I use pow (2, $ len) for the number of loops? why can I solve the problem by converting it to a binary number? This should be a mathematical problem... but I don't want to understand it. please dial it...
The two-digit method is simple. The first thing you need to understand is $ tmp = str_pad (base_convert ($ I, 10, 2), $ len, '0', STR_PAD_LEFT ); in this case, the length is increased by a few zeros based on the length, and then can be cyclically obtained.
If ($ tmp {$ j }== '1 '){
$ T [] = $ arr [$ j];
}
The length of this array can be the same as that of $ len.
Sharp version
6 # Why do I use pow (2, $ len) for the number of loops? why can I solve the problem by converting it to a binary number? This should be a mathematical problem... but I don't want to understand it. please dial it...
The two-digit method is simple. The first thing you need to understand is $ tmp = str_pad (base_convert ($ I, 10, 2), $ len, '0', STR_PAD_LEFT ); in this case, the length is increased by a few zeros based on the length, and then can be cyclically obtained.
If ($ tmp {$ j }== '1 '){
$ T [] = $ arr [$ j];
}
The length of this array can be the same as that of $ len.
A little clue.
Is it true that the binary number is 010101 or the like, which can represent the selected/unselected position?
Pow (2, $ len) can be greater than or equal to the number of all the permutation conditions, but smaller than the number of pow, each of which has a different binary format, so we can use this method for filtering?
I haven't figured it out yet. I don't know how to prove it or how to prove it.
See http://blog.sina.com.cn/s/blog_605f5b4f0100vcwz.html
Sharp version
6 # Why do I use pow (2, $ len) for the number of loops? why can I solve the problem by converting it to a binary number? This should be a mathematical problem... but I don't want to understand it. please dial it...
After 10? Next cycle ???,?? Lower ??? The most efficient ,?? OK? What's xuzuning's boss? Busy
function combination($ar, $num) { $control = range(0, $num-1); $k = false; $total = count($ar); while($control[0] < $total-($num-1)) { $t = array(); for($i=0; $i<$num; $i++) $t[] = $ar[$control[$i]]; $r[] = $t; for($i=$num-1; $i>=0; $i--) { $control[$i]++; for($j=$i; $j<$num-1; $j++) $control[$j+1] = $control[$j]+1; if($control[$i] < $total-($num-$i-1)) break; } } return $r;}