zoukankan      html  css  js  c++  java
  • 28. 字符串的全排列之第2篇[string permutation with repeating chars]

    【本文链接】

     http://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html

    题目】

    输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。例如输入字符串aba,则输出由字符a、b所能排列出来的所有字符串aab、aba、baa。

    分析

    之前的博文28.字符串的排列之第1篇[StringPermutation]中已经介绍了如何对字符串进行全排列,但是只能针对所有字符都不同的情况,如果含有重复字符,则将不再适合。

    因此现在要想办法来去掉重复的排列。如何去掉呢?

    由于全排列就是从第一个字符x起分别与它后面的字符y进行交换。那么如果在x到y之间已经存在和y相等的字符,那么x和y就不应该交换。

    比如对12324:

    (1)第一个数1与第一个数1交换,得到12324;

    (2)第一个数1与第二个数2交换,得到21324;

    (3)第一个数1与第三个数3交换,得到32124;

    (4)第一个数1与第四个数2交换,由于在第一个数和第四个数之间存在一个2和第四个数相等,因此这两个数字不应该交换;

    (5)第一个数1与第五个数4交换,得到42321;

    这样我们总结除了在全排列中去掉重复的规则:去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换

    可以写出如下函数决定2个字符是否进行交换。

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    // judge whether we should swap str[index] and str[i]
    bool IsSwap(char *str, int index, int i)
    {
        
    // char in [index,i) equals to str[i] ?
        // abcb ===> 0,3
        for (int p = index; p < i; p++)
        {
            
    if (str[p] == str[i])
                
    return false;
        }
        
    return true;
    }

    那么在之前的代码基础之上,只需要在交换2个字符之间进行一下判断即可,完整代码和测试用例如下:

    【代码】

     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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
     
    // 28_StringPermutationCombination.cpp : Defines the entry point for the console application.
    //
    /*
        version: 1.0
        author: hellogiser
        blog: http://www.cnblogs.com/hellogiser
        date: 2014/5/25
    */


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

    // swap a and b
    void swap(char &a, char &b)
    {
        
    char t = a;
        a = b;
        b = t;
    }

    // print string
    void Print(char *str)
    {
        
    if (str == NULL)
            
    return;

        
    char *ch = str;
        
    while(*ch)
        {
            cout << *ch;
            ch++;
        }
        cout << endl;
    }

    // abc, abcb
    // judge whether we should swap str[index] and str[i]
    bool IsSwap(char *str, int index, int i)
    {
        
    // char in [index,i) equals to str[i] ?
        // abcb ===> 0,3
        for (int p = index; p < i; p++)
        {
            
    if (str[p] == str[i])
                
    return false;
        }
        
    return true;
    }

    // string permutation recursively
    void Permutation(char *str, int len, int index)
    {
        
    //base case
        if (index == len)
        {
            
    // we have got a permutation,so print it
            Print(str);
            
    return;
        }

        
    for (int i = index; i < len; i++)
        {
            
    // decide whether to swap
            if (IsSwap(str, index, i))
            {
                swap(str[index], str[i]);
                Permutation(str, len, index + 
    1);
                swap(str[index], str[i]);
            }
        }
    }

    // abcb: containing same char in strings
    void StringPermutation(char *str)
    {
        
    if (str == NULL)
            
    return;
        
    int len = strlen(str);
        Permutation(str, len, 
    0);
    }

    //================================================
    //                 test cases                    //
    //================================================
    void test_base(char *str)
    {
        StringPermutation(str);
        cout << 
    "------------------------ ";
    }

    void test_case1()
    {
        
    char str[] = "1";
        test_base(str);
    }

    void test_case2()
    {
        
    char str[] = "123";
        test_base(str);
    }

    void test_case3()
    {
        
    char str[] = "121";
        test_base(str);
    }

    void test_case4()
    {
        
    char str[] = "111";
        test_base(str);
    }

    void test_case5()
    {
        
    char str[] = "1231";
        test_base(str);
    }

    void test_case6()
    {
        
    char str[] = "1221";
        test_base(str);
    }

    void test_main()
    {
        test_case1();
        test_case2();
        test_case3();
        test_case4();
        test_case5();
        test_case6();
    }
    //================================================
    //                 test cases                    //
    //================================================

    int _tmain(int argc, _TCHAR *argv[])
    {
        test_main();
        
    return 0;
    }
    /*
    1
    ------------------------
    123
    132
    213
    231
    321
    312
    ------------------------
    121
    112
    211
    ------------------------
    111
    ------------------------
    1231
    1213
    1321
    1312
    1132
    1123
    2131
    2113
    2311
    3211
    3121
    3112
    ------------------------
    1221
    1212
    1122
    2121
    2112
    2211
    ------------------------
    */

    【参考】

    http://www.cnblogs.com/hellogiser/p/3738844.html

    http://blog.csdn.net/hackbuteer1/article/details/7462447

    【本文链接】

     http://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html

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

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    Vagrant In Windows 10
    Game Console参数指北
    Java并发编程:volatile关键字解析
    自己实现Linkedlist,实现其常用的增、删、查的方法
    自己实现Arraylsit,实现其常用的几种增、删、该、查的方法
    使用@RequestPart同时上传表单数据和文件(转载)
    Springboot配置跨域访问
    Tesseract-OCR安装使用及样本训练
    Java使用tess4J进行OCR图像识别
    SpringBoot中的静态资源访问(转载)
  • 原文地址:https://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html
Copyright © 2011-2022 走看看