字符串的排列 C++实现
2014-03-04 13:07
453 查看
题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来
的所有字符串abc、acb、bac、bca、cab和cba。
的所有字符串abc、acb、bac、bca、cab和cba。
//============================================================================ // Name : PermutationAndCombination.cpp // Author : Lee // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include<deque> using namespace std; void perAndCom(char arr[],int len,deque<char> dq){ if(1==len){ while(dq.size()!=0){ cout<<dq.front(); dq.pop_front(); } cout<<arr[0]<<endl; return; } for(int i=0;i<len;i++){ deque<char> dq_one; for(deque<char>::iterator itr=dq.begin();itr!=dq.end();itr++){ dq_one.push_back(*itr); } dq_one.push_back(arr[i]); char child[len-1]; for(int j=0;j<i;j++){ child[j]=arr[j]; } for(int j=i+1;j<len;j++){ child[j-1]=arr[j]; } perAndCom(child,len-1,dq_one); } } int main() { cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! char test[4]={'a','b','c','d'}; deque<char> dq; perAndCom(test,4,dq); return 0; }
相关文章推荐
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 字符串数组元素排列与组合的Java递归实现
- C++【String类】String插入单个字符,插入字符串的函数实现
- c++实现字符串分隔
- 递归实现 参数字符串中的字符反向排列
- C++实现将一个字符串转化为double类型
- C++ 实现 根据字符串 调用同名函数
- java实现字符串排列组合问题
- 字符串全排列-递归实现
- C++实现一个栈,出栈,入栈,返回最小值 时间复杂度为O(1),查找字符串中第一个重复出现的字符
- C++中用Socket实现对结构体、长字符串和图片的传输
- C++实现十六进制表示字符串
- 十六进制字符串转换成十进制整数的C++实现源代码
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- UTF-8编码的字符串拆分成单字、获取UTF-8字符串的字符个数的代码及原理(c++实现)
- C/C++实现split分割字符串
- 生成1到n的随机排列(C++实现)
- C++实现从输入中读取字符串
- 递归实现将参数字符串中的字符反向排列,要求不能使用C函数库中的字符串操作函数
- "找出字符串中第一个不重复的字符"和"找出字符串中最后一个重复的字符"的两种方法的C++实现