Home > Back-end >  Beg god give me answers about how to use the back in the implementation, gyrus luca dead can't
Beg god give me answers about how to use the back in the implementation, gyrus luca dead can't

Time:12-25

Currently has an array of letters: A, D, F, Z, K, L, O], to find out all of its subsets, such as the subset have [A], [A, D], [Z,
L, O], [D, F, K, O], etc., please use the back in the search algorithm, according to the required output:
Length of 0 subset are: 1
Length of 1 subsets are: 7
Length is a subset of the 2:21
Length is 3 a subset of the...
...
Length is 6 subset are: 7
Length is 7 subset are: 1

CodePudding user response:

 # include & lt; Vector> 
# include & lt; Iostream>

Void BackTrackSubArrays (STD: : vector & VecTemp, size_t & amp; NCount, size_t nTotal, size_t nBegin, const STD: : vector & VecArray)
{
If (vecTemp. The size ()==nTotal)
{
+ + nCount;
return;
}

For (size_t I=nBegin; I & lt; VecArray. The size (); I++)
{
VecTemp. Push_back (vecArray [I]);
BackTrackSubArrays (vecTemp, nCount nTotal, I + 1, vecArray);
VecTemp. Pop_back ();
}
}

Void SubArrays (const STD: : vector & VecArray)
{
STD: : vector VecTemp;
VecTemp. Reserve (vecArray. The size ());
Size_t nCount=0;
For (size_t I=0; I & lt;=vecArray. The size (); I++)
{
BackTrackSubArrays (vecTemp, nCount, I, 0, vecArray);
STD: : cout & lt; <"Length" & lt; NCount=0;
}
}

Int main ()
{
SubArrays ({' A ', 'D', 'F' and 'Z', 'K', 'L', 'O'});//use c + + 11 initializes the syntax here, if the compiler doesn't support c + + 11, please your STD: : vector Enter
system("pause");
return 0;
}
  • Related