zoukankan      html  css  js  c++  java
  • [笔试题]字符串的排列和组合

    【代码】

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
     
    /*
        version: 1.0
        author: hellogiser
        blog: http://www.cnblogs.com/hellogiser
        date: 2014/9/22
    */


    #include "stdafx.h"
    #include "iostream"
    #include <vector>
    using namespace std;

    void print(char *str)
    {
        cout << str << endl;
    }

    void swap(char *a, char *b)
    {
        
    char t = *a;
        *a = *b;
        *b = t;
    }

    //======================================================
    // string permutation
    // abc =======abc,acb,bac,bca,cab,cba
    //======================================================
    void permutation(char *str, int len, int index)
    {
        
    if (index == len)
        {
            print(str);
        }
        
    else
        {
            
    for (int i = index; i < len; i++)
            {
                swap(str[index], str[i]);
                permutation(str, len, index + 
    1);
                swap(str[index], str[i]);
            }
        }
    }

    void Permutation(char *str)
    {
        
    if(str == NULL || *str == '')
            
    return;
        
    int len = strlen(str);
        permutation(str, len, 
    0);
    }

    //======================================================
    // string combination
    // abc =========a,b,c,ab,ac,bc,abc
    //======================================================
    void print(vector<char> &result)
    {
        vector<
    char>::iterator iter = result.begin();
        
    for (; iter != result.end(); iter++)
        {
            cout << *iter;
        }
        cout << endl;
    }

    void combination(char *str, int m, vector<char> &result)
    {
        
    // exception  for example   C(1,2)
        if(str == NULL || *str == '' && m > 0)
            
    return;

        
    //base cases
        if (m == 0)
        {
            print(result);
            
    return;
        }
        
    //choose current char   C(n-1,m-1)
        result.push_back(*str);
        combination(str + 
    1, m - 1, result);

        
    // not choose current char  C(n-1,m)
        result.pop_back();
        combination(str + 
    1, m, result);
    }

    void Combination(char *str)
    {
        
    if(str == NULL || *str == '')
            
    return;
        vector<
    char> result;
        
    int len = strlen(str);
        
    for (int i = 1; i <= len; i++)
        {
            combination(str, i, result);
        }
    }

    void test_permutation()
    {
        
    char str[] = "abc";
        Permutation(str);
    }

    void test_combination()
    {
        
    char str[] = "abc";
        Combination(str);
    }

    void test_main()
    {
        test_permutation();
        cout << 
    "========================== ";
        test_combination();
    }

    int main(void)
    {
        test_main();
        
    return 0;
    }
    /*
    abc
    acb
    bac
    bca
    cba
    cab
    ==========================
    a
    b
    c
    ab
    ac
    bc
    abc
    */

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    客户端rsyslog配置文件详解
    logstash插件配置-codec插件说明json和multiline
    Linux流量监控工具
    单线程 Redis 为什么这么快,看看这篇就知道了
    RabbitMQ Network Partitions 处理策略
    inux 下配置网卡的别名即网卡子IP的配置 转
    Prometheus的监控解决方案(含监控kubernetes)
    Prometheus+Grafana监控Kubernetes
    python输入一维数组(输入以空格为间隔的一行)
    用gdb来理解:值传递/指针传递/引用传递
  • 原文地址:https://www.cnblogs.com/hellogiser/p/string-combination-permutation.html
Copyright © 2011-2022 走看看