字符串全排列
问题:给定字符串S,生成该字符串的全排列。
方法:依次从字符串中取出一个字符作为最终排列的第一个字符,对剩余字符组成的字符串生成全排列,最终结果为取出的字符和剩余子串全排列的组合:
#include <iostream>
#include <string>
using namespace std;
void permute(string prefix, string str)
{
if(str.length() == 0)
cout << prefix << endl;
else
{
for(int i = 0; i < str.length(); i++)
permute1(prefix+str[i], str.substr(0,i)+str.substr(i+1,str.length()));
}
}
int main(){
permute("",s);
return 0;
}
具体过程如图所示: