Recursion 8.4
2016-03-22 09:13
344 查看
Write a method to compute all permutations of a string.
// ===================================================================================== // // Filename: 8_4.cpp // // Description: a method to compute all permutations of a string // // Version: 1.0 // Created: 03/21/2016 09:53:33 AM // Revision: none // Compiler: g++ // // Author: // Organization: // // ===================================================================================== #include <iostream> #include <string> #include <list> void permutations(const std::string& prefix,const std::string& str, std::list<std::string>& str_list){ if(str.empty()) { str_list.push_back(prefix); return; } for(int i=0; i<str.size(); i++){ std::string str_tmp(str); std::string prefix_tmp(prefix); if(str.find_first_of(str[i])!=i) continue; permutations(prefix_tmp.append(1, str[i]), str_tmp.erase(i,1), str_list); } } std::list<std::string> permutations(const std::string& str){ std::list<std::string> str_list; permutations("", str, str_list); return str_list; } // === FUNCTION ====================================================================== // Name: main // Description: // ===================================================================================== int main ( int argc, char *argv[] ) { std::list<std::string> str_list = permutations("1224"); std::cout << str_list.size() << std::endl; for(auto it = str_list.begin(); it!=str_list.end(); it++){ std::cout << *it << std::endl; } return 0; } // ---------- end of function main ----------
相关文章推荐
- 还原数据库提示“ 因为数据库正在使用,所以无法获得对数据库的独占访问权“
- 火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件
- IOS应用程序发布到苹果APP STORE完整流程使用教程
- Java实现把excel xls中数据转为可直接插入数据库的sql文件
- LoadRunner12.x简易入门教程--(四)关联的使用
- chrom_极客快捷键
- camShift跟踪算法介绍
- 使用VTS分析bacnet协议的ReadProperty(二)
- 10、ExtJS入门教程【学习笔记】
- ios打包ipa的四种实用方法(.app转.ipa)
- crontab命令详解
- 用IE打开一个网页
- eclipse设置背景颜色、字体大小、行号、XML字体大小和颜色等操作
- docker使用记录
- 137.7万个组织使用移动办公产品 数解最爱的那些功能
- 《Python数据分析基础教程:Numpy学习指南》- 速记 - 第二章
- PBR综合小实验视频-狮子XL
- 一个程序员的蜕变(我是如何成为架构师的)
- 15款基于 jQuery模态对话框
- win7 svchost.exe 占用内存cpu过高