C++、python 写类似全排列算法
2016-05-21 20:03
375 查看
C++的类似全排列算法
有['a','b','c']、['1','2']、['A','B','C'] 三个数组,依次从数组中取一个字符,把所以的可能性全都列举出来。共有3*2*3=18中情况。我使用的是递归方法。当然这是我举的例子,实际中数组个数,以及每个数组中的元素个数是不确定的。代码如下:
//循环递归 /* vec 原始的vector cur_indx 当前遍历的第cur_indx+1个vector cur_vec_indx 第cur_indx+1个vector的第cur_vec_index+1 个元素 */ void do_digui(vector<vector<string> > &vec,int cur_indx,vector<string > &tmp,vector<vector<string> > &ret) { <span style="white-space:pre"> </span>if (cur_indx >= vec.size()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>return ; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>vector<string> cur_vec = vec[cur_indx]; <span style="white-space:pre"> </span>int size=cur_vec.size(); <span style="white-space:pre"> </span>for (int i = 0; i < size; i++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>vector<string > t = tmp; <span style="white-space:pre"> </span>t.push_back(cur_vec[i]); <span style="white-space:pre"> </span>if(vec.size() == (cur_indx + 1))//已经取到vec中的最后一个数据了,需要把前面遍历的结果保存起来。 <span style="white-space:pre"> </span>ret.push_back(t); <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>loopCandidateVector_1(vec, cur_indx+1, t, ret); <span style="white-space:pre"> </span>} }
调用方式如下:
vector<vector<string> > vec_data; vector<string > vec1; vector<string > vec2; vector<string > vec3; vec1.push_back("a"); vec1.push_back("b"); vec1.push_back("c"); vec2.push_back("1"); vec2.push_back("2"); // vec2.push_back("3"); vec3.push_back("A"); vec3.push_back("B"); vec3.push_back("C"); vec_data.push_back(vec1); vec_data.push_back(vec2); vec_data.push_back(vec3); vector<string > tmp; vector<vector<string> > ret; do_digui(vec_data, 0, tmp, ret); for (int i = 0; i < ret.size(); i++) { vector<string > tm = ret[i]; for (int j = 0; j < tm.size(); j++) { cout << tm[j] << " "; } cout << endl; }
结果如下图:
原理是什么?
其实就是网上一般搜的全排列的原理。
下面在粘贴一个python版的,让以后自己好查阅
def stripEmptyList(list): for i in range(0,len(list)): if(len(list[i])<=0): list=list[0:i] break return list def doLoop(source_list,index,tmp_list,result_list): <span style="white-space:pre"> </span>list_1=source_list[index] <span style="white-space:pre"> </span>for item in list_1: <span style="white-space:pre"> </span>tmp=tmp_list[:] <span style="white-space:pre"> </span>tmp.append(item) <span style="white-space:pre"> </span>if(len(source_list)==(index+1)): <span style="white-space:pre"> </span>result_list.append(tmp) <span style="white-space:pre"> </span>else: <span style="white-space:pre"> </span>doLoop(source_list,(index+1),tmp,result_list) def Loop(source_list): tmp_list=[] result_list=[] index=0 doLoop(source_list,index,tmp_list,result_list) return result_list if __name__ == '__main__': list=[['1','2','3'],['a','b'],['A','B','C','D','E'],[],[]] list=stripEmptyList(list) ret=Loop(list) for item in ret: print itemps:刚使用python不到2个月,虽然python代码写起来比较爽,但是一些与C++不同的地方,对我这个新手菜鸟,在编写代码过程中真的是烦人的。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- 关于指针的一些事情
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例