相关疑难解决方法(0)

如何在不使用递归的情况下找到字符串的所有排列?

有人可以帮我解决这个问题:这是一个查找任意长度字符串的所有排列的程序。需要相同的非递归形式。(最好有C语言实现)

using namespace std;

string swtch(string topermute, int x, int y)
{
  string newstring = topermute;
  newstring[x] = newstring[y];
  newstring[y] = topermute[x]; //avoids temp variable
  return newstring;
}

void permute(string topermute, int place)
{
  if(place == topermute.length() - 1)
  {
    cout<<topermute<<endl;
  }
  for(int nextchar = place; nextchar < topermute.length(); nextchar++)
  {
    permute(swtch(topermute, place, nextchar),place+1);
  }
}

int main(int argc, char* argv[])
{    
  if(argc!=2)    
  {
    cout<<"Proper input is 'permute string'";
    return 1;
  }
  permute(argv[1], 0);
  return 0;    
}
Run Code Online (Sandbox Code Playgroud)

c++ iteration algorithm recursion

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

algorithm ×1

c++ ×1

iteration ×1

recursion ×1