//固定递归的出口,就是只剩一个字符的时候,递归的循环过程,就是从每个子串的第二个字符开始依次与第一个字符交换,然后继续处理子串
public function run($args)
{
@set_time_limit(0);
@ini_set('memory_limit', '2048M');
$str = $args[0];
$res = $this->changeStr($str,0,strlen($str)-1);
var_export($res);
}
public function changeStr($str,$p,$max){
static $result = array();
if($p == $max){
if(!in_array($str,$result)){
echo $str."\n";
$result[] = $str;
}else{
return;
}
}else{
for($i = $p;$i <= $max;$i++){
if($str[$i] != $str[$i+1]){
$str = $this->swap($str,$p,$i);
$this->changeStr($str,$p+1,$max);
$str = $this->swap($str,$p,$i);
}
}
}
return $result;
}
public function swap($str,$curp,$index){
$tmp = $str[$index];
$str[$index] = $str[$curp];
$str[$curp] = $tmp;
return $str;
}
public function run($args)
{
@set_time_limit(0);
@ini_set('memory_limit', '2048M');
$str = $args[0];
$res = $this->changeStr($str,0,strlen($str)-1);
var_export($res);
}
public function changeStr($str,$p,$max){
static $result = array();
if($p == $max){
if(!in_array($str,$result)){
echo $str."\n";
$result[] = $str;
}else{
return;
}
}else{
for($i = $p;$i <= $max;$i++){
if($str[$i] != $str[$i+1]){
$str = $this->swap($str,$p,$i);
$this->changeStr($str,$p+1,$max);
$str = $this->swap($str,$p,$i);
}
}
}
return $result;
}
public function swap($str,$curp,$index){
$tmp = $str[$index];
$str[$index] = $str[$curp];
$str[$curp] = $tmp;
return $str;
}