[Pen questions] arrange and combine strings

Source: Internet
Author: User

[Code]

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 = * 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
*/

[Pen questions] arrange and combine strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.