字符串全排列

/*
方法1:依次从字符串中取出一个字符作为最终排列的第一个字符,对剩余字符组成的字符串生成全排列,
最终结果为取出的字符和剩余子串全排列的组合。


#include <iostream>
#include <string>
using namespace std;
  
void permute1(string prefix, string str)
{
  if(str.length() == 0)	//到后缀str的长度为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()));
  }
}
  
void permute1(string s)
{
  permute1("",s);
}
  
int main()
{
  //method1, unable to remove duplicate permutations.
  cout << "method1" << endl;
  permute1("ABA");
}
优点:该方法易于理解,但无法移除重复的排列,如:s="ABA",会生成两个“AAB”。

利用交换的思想,具体见实例,但该方法不如上述方法容易理解
	比如ABC 
	ABC -- A和A交换ABC  --B和C交换  ACB 
	 	-- A和B交换BAC  --A和C交换  BCA 
		-- A和C交换CBA  --B和A交换  CAB 
*/ 
//#include <string.h>
#include <stdio.h>
  
void swap(char* x, char* y)
{
	char tmp;
	tmp = *x;
	*x = *y;
	*y = tmp;
}
  
/* Function to print permutations of string
  This function takes three parameters:
  1. String
  2. Starting index of the string
  3. Ending index of the string. */
void permute(char *a, int i, int n)
{
	int j;
	if (i == n)
		printf("%s\n", a);
	else
		for (j = i; j <= n; j++)//为避免生成重复排列,当不同位置的字符相同时不再交换
		{
	 		if(a[i] == a[j] && j != i){
	 			continue;
			}
			swap((a+i), (a+j));
			permute(a, i+1, n);
			swap((a+i), (a+j)); //backtrack  为了下次另一种交换得换回去 
		}
} 
  
int main()
{
  //method2
  //cout << "method2" << endl;
  char a[] = "ABA";
  permute(a,0,2);
  return 0;
}
/*  acm应用:
题目描述:  
    给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列。  
    我们假设对于小写字母有'a' < 'b' < ... < 'y' < 'z',而且给定的字符串中的字母已经按照从小到大的顺序排列。  
    输入:  
    输入只有一行,是一个由不同的小写字母组成的字符串,已知字符串的长度在1到6之间。  
    输出:  
    输出这个字符串的所有排列方式,每行一个排列。要求字母序比较小的排列在前面。字母序如下定义:  
    已知S = s1s2...sk , T = t1t2...tk,则S < T 等价于,存在p (1 <= p <= k),使得  
    s1 = t1, s2 = t2, ..., sp - 1 = tp - 1, sp < tp成立。  
    样例输入:  
    abc  
    样例输出:  
    abc  
    acb  
    bac  
    bca  
    cab  
    cba  
    提示:  
    每组样例输出结束后要再输出一个回车。 
#include <stdio.h> 
 #include <stdlib.h> 
 #include <string.h> 
   
 struct seq 
 { 
   char str[7]; 
 }; 
   
 struct seq seqs[721]; 
 int count; 
   
 int is_swap(char *str, int begin, int k) 
 { 
   int i, flag; 
   
   for (i = begin, flag = 1; i < k; i ++) { 
     if (str[i] == str[k]) { 
       flag = 0; 
       break; 
     } 
   } 
   
   return flag; 
 } 
   
 void swap(char *str, int a, int b) 
 { 
   char temp; 
   temp = str[a]; 
   str[a] = str[b]; 
   str[b] = temp; 
 } 
   
 void permutation_process(char *name, int begin, int end) { 
   int k; 
   
   if (begin == end - 1) { 
     strcpy(seqs[count].str, name); 
     count ++; 
   }else { 
     for (k = begin; k < end; k ++) { 
       if (is_swap(name, begin, k)) { 
         swap(name, k, begin); 
         permutation_process(name, begin + 1, end); 
         swap(name, k, begin); 
       } 
     } 
   } 
 } 
   
 int compare(const void *p, const void *q) 
 { 
   const char *a = p; 
   const char *b = q; 
   return strcmp(a, b); 
 } 
   
 int main() 
 { 
   char name[7]; 
   int i, len; 
   
   while (scanf("%s", name) != EOF) { 
     count = 0; 
     len = strlen(name); 
     permutation_process(name, 0, len); 
     qsort(seqs, count, sizeof(seqs[0]), compare); 
   
     for (i = 0; i < count; i ++) { 
       printf("%s\n", seqs[i].str); 
     } 
     printf("\n"); 
   } 
   
   return 0; 
 } 
*/
 

算法系列操作全部参考自网上资料

比如:

http://blog.csdn.net/naruto2011sasuke/article/details/46634525
http://blog.csdn.net/han_xiaoyang/article/details/12163251#
http://blog.csdn.net/naruto2011sasuke/article/details/46634525
http://www.ahalei.com/forum-63-1.html等等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值