Recursion 8.4

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  ----------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值