[算法] 字符串的全排列 [dfs - 递归神技]

字符串的全排列,递归交换
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>
#include <climits>
#include <cassert>
#define BUG puts("here!!!");

using namespace std;
const int N = 1005;
void swap(char &a, char &b) {
	char temp = a;
	a = b;
	b = temp;
}
void f(char *str, char *begin) {
	if(*begin == '\0') {
		cout << str << endl;
		return;
	}
	for(char *p = begin; *p != '\0'; p++) {
		swap(*p, *begin);
		f(str, begin+1);
		swap(*p, *begin);
	}
}	
int main() {
	char s[10] = "abcde";
	f(s, s);
	return 0;
}

posted @ 2013-02-06 11:15  小尼人00  阅读(274)  评论(0编辑  收藏  举报