複試上級指南之遞歸字符串全排列

北京大學複試上級題

給一個由不同小寫字母組成的字符串,輸出字符串的全排列。

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char str[10001];
void Permutation(int from,int to)
{
  if(from==to)
  {
     for(int i=0;i<=to;i++)
     {
	cout<<str[i];
     }
     cout<<'\n';
     return;
  }
  for(int i=from;i<=to;i++)
  {
     swap(str[i],str[from]);//交換
     Permutation(from+1,to);
     swap(str[i],str[from]);//還原
  }
}
 
int main()
{
  cin>>str;
  int size =strlen(str);
  Permutation(0,size-1);
  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章