[php]全排列非递归置换法
时间:2011-01-17 来源:一缕青烟
<?php function appendItem( $c , &$arr ) { if(!$arr) $arr[] = $c; else { $p = $arr; $arr = array(); foreach( $p as $k=>$v) { $arr = array_merge($arr , walkString($c,$v) ); } } } function walkString( $char , $string ) { $mt[] = $ns = $string.$char; $sl = strlen( $string ); while( $sl ) { $tmp = $ns{$sl}; $ns{$sl} = $ns{$sl-1}; $ns{$sl-1} = $tmp; $mt[] = $ns; $sl--; } return $mt; } function combine( $array ) { $len = count( $array ); $arr = array(); for( $i=0 ; $i < $len ; $i++ ) { appendItem( $array[$i] , $arr ); } return $arr; } set_time_limit( 0 ); $arr = str_split('abcd'); print_r( combine($arr)); ?>
相关阅读 更多 +