[php]全排列非递归置换法

网络整理 - 08-29

<?phpfunction 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));?>