【100题】第五十三题 字符串的全排列
来源:互联网 发布:网络爬虫软件排名 编辑:程序博客网 时间:2024/05/22 13:24
递归求解思路:
1) 每个元素依次放到首位,然后对其余元素递归
2) 当当前元素到达末尾的时候,输出该序列
关键是:
每个元素交换完,之后要交换过来。每个元素依次放到首位,
for(int i=currentIndex;i<=n;++i){ swap 从i+1递归 swap }
#include<stdio.h>#include<stdlib.h>#define SWAP(x,y,t)((t)=(x),(x)=(y),(y)=(t))int score=0;void perm(int *list,int i,int n){ int j,temp; if(i==n) { for(j=0;j<=n;j++) { printf("%d ",list[j]); } printf("\n"); score++; } else { for(j=i;j<=n;j++) { SWAP(list[i],list[j],temp); perm(list,i+1,n); SWAP(list[i],list[j],temp); } }}int main(){ int list[]={1,2,3}; perm(list,0,2); printf("Thetotal number is %d\n",score); system("pause");}
String 版本
#include <iostream>using namespace std; void Permutation(string pStr, int k, int n){ if(k==n) cout<<pStr<<endl; for(int i=k;i<n;++i) { swap(pStr.at(i),pStr.at(k)); Permutation(pStr, k+1,n); swap(pStr.at(i),pStr.at(k)); } } int main(){ strings="abcdef"; Permutation(s,0,s.length());}
二,扩展
如果不是求字符的所有排列,而是求字符的所有组合,应该怎么办呢?(P72)
思路:这里不采用交换方式,而是采用删减的方式。采用不同的剔除顺序,并用prex保留删减值,这样将得到所有符合条件的序列
#include <iostream>using namespacestd;voidselect(string str, string prex){ string temp=str; cout<<prex<<endl; for(int i=0;i<str.length();++i) { select(str.erase(i,1),prex+str.at(i)); str=temp; } } int main(){ string s="abcd"; select(s,"");}
扩展二:
当输入的字符串中含有相同的字符串时,相同的字符交换位置是不同的排列,但是同一个组合。
举个例子,如果输入aaa,那么它的排列是6个aaa,但对应的组合只有一个。
- 【100题】第五十三题 字符串的全排列
- 第五十三题(字符串的排列)
- 字符串的排列 【微软面试100题 第五十三题】
- 【算法题】字符串的全排列
- 算法题:字符串的全排列
- 第七十题(字符串的全排列)
- 算法题:字符串的全排列
- CVTE笔试题---字符串的全排列
- 微软100题(70)字符串的全排列
- 笔试题:字符串全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 多线程 MessageBox.Show()问题
- 遇见C++ AMP:在GPU上做并行计算
- 《Unix环境高级编程》:演示不同的exit值
- MySQL对性能较为强劲的服务器利用率测试
- 线性筛素数
- 【100题】第五十三题 字符串的全排列
- hdu 1556 Color the ball(线段树的另一种应用)
- 在ubuntu上安装code blocks 10.05版本的方法
- 【100题】第五十四题 调整数组顺序使奇数位于偶数前面
- x86_64 调用约定
- 重构之我见
- 2012年8月22日
- 计算机端口详细介绍(整理版)
- c#的default、using和yield使用小结