按字典序生成字符串的全排列
直接递归:
#include <iostream>
#include <string>
#include <cstring> using namespace std; int len;
bool ever[9];
string str; void permutation(string cur)
{
if(cur.size() == len)
{
cout<<cur<<endl;
return ;
}
for(int i=0;i<len;++i)
{
if(!ever[i])
{
ever[i] = 1;
permutation(cur + str[i]);
ever[i] = 0;
}
}
} int main()
{
while(cin>>str)
{
memset(ever,0,sizeof(ever));
len = str.size();
permutation("");
}
return 0;
}