55. Two Methods for string combination [string combination]

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/string-combination.html

Question]

Question: enter a string to output all combinations of Characters in the string. For example, if abc is input, its combination includes a, B, c, AB, ac, bc, and abc.

Analysis]

Previous blog posts28. String arrangement [StringPermutation]It discusses how to use recursive methods to find the string arrangement. Likewise, you can use recursive methods to obtain a string combination.

 [Recursive Method for combination]

Consider the combination of m characters in a string with a length of n, and set it to C (n, m ). The solution to the original problem is the sum of C (n, 1), C (n, 2),... C (n, n. For C (n, m), Scanning starts from the first character. Each character can be either selected or not selected. If selected, recursive solution C (N-1 m-1); if not selected, recursive solution C (n-1, m ). In either case, the value of n is reduced, and the recursive termination condition n is 0 or m = 0.

[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
  // 55_StringCombination.cpp: Defines the entry point for the console application.
//
/*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date:
*/

# Include "stdafx. h"
# Include <vector>
# Include <iostream>
Using namespace std;

// Print string combination
Void Print (vector <char> & result)
{
Vector <char >:: const_iterator iterBegin = result. begin ();
Vector <char>: const_iterator iterEnd = result. end ();

For (; iterBegin! = IterEnd; ++ iterBegin)
Printf ("% c", * iterBegin );
Printf ("\ n ");
}

// Get string combination recursively
// Choose m chars from str
Void Combination (char * str, unsigned int m, vector <char> & result)
{
If (NULL = str | (* str = '\ 0' & m> 0 ))
Return;

// Base cases
If (m = 0)
{
// We have got a combination, print it
Print (result );
Return;
}
// (1) choose current char
Result. push_back (* str );
// Choose M-1 chars from remaining n-1 chars
Combination (str + 1, m-1, result );

// (2) not choose current char
Result. pop_back ();
// Choose m chars from remaining n-1 chars
Combination (str + 1, m, result );
}

// String combination
Void StringCombination (char * str)
{
If (NULL = str | * str = '\ 0 ')
Return;
Int len = strlen (str );
Vector <char> result;
For (int I = 1; I <= len; ++ I)
Combination (str, I, result );
}

Void test_base (char * str)
{
StringCombination (str );
Printf ("--------------------- \ n ");
}

Void test_case1 ()
{
Char str [] = "";
Test_base (str );
}

Void test_case2 ()
{
Char str [] = "";
Test_base (str );
}

Void test_case3 ()
{
Char str [] = "abc ";
Test_base (str );
}

Void test_main ()
{
Test_case1 ();
Test_case2 ();
Test_case3 ();
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
Test_main ();
Return 0;
}
/*
---------------------
A
---------------------
A
B
C
AB
Ac
Bc
Abc
---------------------
*/

Because the combination can be a combination of 1 character, a combination of 2 characters ...... Until the combination of n characters, a for loop is required in the void StringCombination (char * str) function. In addition, a vector is used to store the characters that you choose to put into the combination.

 Bitwise operation for Combination]

In addition, this question also provides a clever way of thinking, which can be used to find combinations based on bitwise operations. A binary number is used to determine the character selection. If one digit is 1, the corresponding character is obtained. If the value is 0, the character combination can be realized.

For example, if the length of "abc" is 3, there are seven possible combinations. Let num increase from 1 to 7, and judge whether to choose from each character.

For example: num = 1, that is, 001:

(1) j points to 1st characters, (a> j) & 1 = 1, then;

(2) j points to 2nd characters. (a> j) & 1 = 0, B is discarded;

(3) j points to 3rd characters. (a> j) & 1 = 0, c is discarded;

The string of this combination is;

And so on.

When num = 7, that is, 111:

(1) j points to 1st characters, (a> j) & 1 = 1, then;

(2) j points to 2nd characters. (a> j) & 1 = 1, B is used;

(3) j points to 3rd characters. (a> j) & 1 = 1, c is used;

The string of this combination is abc;

Then, when num extracts all values in turn, all strings can be combined.

[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
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date:
*/
Void StringCombinationUsingBitwise (char * str)
{
// Use bitwise operations to get string combination
If (NULL = str | * str = '\ 0 ')
Return;
Int len = strlen (str );
If (len> = 32)
Return;
Int sum = 1 <len;
For (int I = 1; I <sum; ++ I)
{
For (int j = 0; j <len; j ++)
{
If (I> j) & 0x1)
{
// Choose char at str [j]
Printf ("% c", str [j]);
}
}
Printf ("\ n ");
}
}

Compared 【Recursive Method for combination],Bitwise operation for Combination]The speed is faster. The time complexity is T = n * 2n, but n cannot exceed 32.

[Reference]

Http://zhedahht.blog.163.com/blog/static/2541117420114172812217/

Http://zhuyanfeng.com/archives/3246

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

Http://blog.csdn.net/wuzhekai1985/article/details/6643127

 [Link to this article]

Http://www.cnblogs.com/hellogiser/p/string-combination.html

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.