字符串去重全排列

字符串的去重全排列

  • 应用场景暴力破解填空题目
  • 全排列
        #define SWAP(x,y,t) ((t) = (x),(x) = (y),(y) = (t))//用于交换两个数 使用宏定义最好加上()以免引起歧义
	
	bool isSwap(char *list, int begin, int end) { //判断是否相同以决定是否要交换
		for (int i = begin; i < end; i++){
			if (list[i] == list[end])
				return false;
		}
		return true;
	}
	
	void perm(char *list, int i, int n){ //递归写法求全排列
		int j, temp;
		if (i == n) { //递归出口
			printf("	%s
", list);
		}
		else {
			for (j = i; j <= n; j++){
				if (isSwap(list, i, j)) { //不去重的话就是组合的形式
					//使用宏定义,传的是数值,如果这的swap用函数实现,传的应该是指针
					 SWAP(list[i], list[j], temp);
					//交互位置后,输出以list[j]不变,后面的字母改变的所有排列
					perm(list, i + 1, n);
					SWAP(list[i], list[j], temp);
				}   
			}
		}
	}

调用方式:
char list[] = {'a', 'b', 'c', 'd'};//{'1', '2', '3', '2'} 处理数字版本
perm(list, 0, strlen(list) - 1)

原文地址:https://www.cnblogs.com/DengSchoo/p/12606915.html