【本文链接】

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 + );
            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, );
}

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

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();
    ;
}
/*
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

28. 字符串的全排列之第2篇[string permutation with repeating chars]的更多相关文章

  1. 28. 字符串的排列之第1篇[StringPermutation]

    [题目] 输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab和cba. [分析] 这是一道很好的 ...

  2. 剑指Offer26 字符串的全排列

    /************************************************************************* > File Name: 26_String ...

  3. 【算法与数据结构】Java实现字符串的全排列及组合

    注:本文记录了代码编写及调试过程,想直接浏览正确答案的请移步文章结尾. 一.字符串的全排列问题 1. 下面是最初的代码(答案有错误-重复输出) import java.util.Scanner; pu ...

  4. python3:实现字符串的全排列(有重复字符)

    抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321. 解决方案 #字符串任意两个位置字符交换 def str_replace(str, x ...

  5. python3:实现字符串的全排列(无重复字符)

    最近在学一些基础的算法,发现我的数学功底太差劲了,特别是大学的这一部分,概率论.线性代数.高数等等,这些大学学的我是忘得一干二净(我当时学的时候也不见得真的懂),导致现在学习算法,非常的吃力.唉!不说 ...

  6. python3实现字符串的全排列的方法(无重复字符)

    https://www.jb51.net/article/143357.htm 抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321.(暂时 ...

  7. PHP基础(一)--字符串函数大盘点(基础篇)

    参考地址http://php.net/manual/zh/ref.strings.php addcslashes - 以 C 语言风格使用反斜线转义字符串中的字符    string addcslas ...

  8. C++版 - 剑指offer面试题28: 字符串的排列

    题目: 字符串的排列 热度指数:5777 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出 ...

  9. Redis系列-存储篇string主要操作命令

    Redis系列-存储篇string主要操作命令 通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简 ...

随机推荐

  1. shell--题目

    1.有一个文件,里面有二列,第一列ip地址,第二列是时间,同一个ip可能出现多次,但时间不同. 文件类似下面的样子: 192.168.1.2              13:10 192.127.12 ...

  2. Fedora和Ubuntu下安装OpenGL开发环境配置

    Fedora下OpenGl开发环境配置 开发OpenGL工程需要3个库文件和对应的头文件: libglut.so,libGLU.so,libGL.so, gl.h ,glu.h, glut.h 这些库 ...

  3. spring事务学习(转账案例)(二)

    四.通过springAop进行事务管理 继续从第一个无事务操作的项目中进行更改. 只修改applicationContext.xml配置文件,注意设置transaction引用 <?xml ve ...

  4. Spring IoC、DI入门小程序

    Alt+/智能提示xml配置文件节点及属性:在接口上使用Ctrl+T可以提示其实现类 一.IoC控制反转(将创建对象的权利交给spring)入门小程序 1.引入jar包 2.工程基本结构 3.新建Us ...

  5. php 数组二分法查找函数

    找到返回对应的key,找不到返回-1,注意二分查找需要数组有序,下边函数需要数组递增排序. function binarySearch($arr,$x){ $start=0; $end=count($ ...

  6. Python代码性能优化技巧

    摘要:代码优化能够让程序运行更快,可以提高程序的执行效率等,对于一名软件开发人员来说,如何优化代码,从哪里入手进行优化?这些都是他们十分关心的问题.本文着重讲了如何优化Python代码,看完一定会让你 ...

  7. Asp.net面试题

    Asp.net核心技术思想 1.概述反射和序列化 反射:程序集包含模块,而模块包含类型,类型又包含成员.反射则提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对 ...

  8. CSS 定义上划线、下划线、删除线代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. php面试题之五——PHP综合应用(高级部分)

    五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transfer Protocol,文件传输协议,是应用层的 ...

  10. Microsoft.ReportViewer.WebForms, Version=10.0.0.0的报错问题,解决方案

    未能加载文件或程序集,或者web.config报错! 已解决:直接找到(默认在 路径/Microsoft Visual Studio 8/ReportViewer).把里面的3个DLL传上去就OK了! ...