博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[笔试题]字符串的排列和组合
阅读量:4353 次
发布时间:2019-06-07

本文共 2608 字,大约阅读时间需要 8 分钟。

【代码】

 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 == 
'\0'
)
        
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 == 
'\0'
 && 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 == 
'\0'
)
        
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 << 
"==========================\n"
;
    test_combination();
}
int
 main(
void
)
{
    test_main();
    
return
 
0
;
}
/*
abc
acb
bac
bca
cba
cab
==========================
a
b
c
ab
ac
bc
abc
*/

转载于:https://www.cnblogs.com/hellogiser/p/string-combination-permutation.html

你可能感兴趣的文章
Vue开发微信H5 微信分享签名失败问题解决方案
查看>>
Linux - 配置SSH免密通信 - “ssh-keygen”的基本用法
查看>>
Python(2.7.6) glob - 匹配指定模式的文件
查看>>
HTTP - 持久连接
查看>>
添加路由时啥时候是dev啥时候是gw
查看>>
redis 中文字符显示
查看>>
登录日志分析常用查询
查看>>
Codeforces Round #228 (Div. 1) 388B Fox and Minimal path
查看>>
【nosql实现企业网站系列之一】mongodb的安装
查看>>
短信服务供应商价格总览
查看>>
获取本机IP(考虑多块网卡、虚拟机等复杂情况)
查看>>
笔记之_java整理ORM框架
查看>>
CentOS下安装python3.x版本
查看>>
CAP定理(原则)以及BASE理论
查看>>
「玩转树莓派」搭建属于自己的云盘服务
查看>>
有道语料库爬虫
查看>>
VS2019 实用设置
查看>>
for循环语句之求和,阶乘,求偶,求n次篮球蹦起高度
查看>>
CFileDialog
查看>>
[转载]EXTJS学习
查看>>