Recursive base - the number of combinations / the number of permutations - output ways (DFS / Recursive / BFS)

B: part of the problem and *** (! = Any note section and sub-section sum is not the same)

Describe   to you the number N, ask if you can remove a number from them, so that their sum is K.

Entry

The first line contains two numbers, N, K, representing the number of digits, and K. Next, and as N rows, each row a number.

Export

If you can pick out a few and the number is K, output YE5, otherwise, the output N0

Sample

Input:

4 0

1 -1  2  3

Output:

YE5

Input:

2 2

1 -3

Output:

N0

This question and find the number of combinations, pay attention to pe outside, thinking a basis for comparison, many ways, the idea as a way to achieve using dfs

If you find the number of possible combinations of output, direct recursion is not enough , so smart traversal, you need a media array, store every possible , because every time things will return to the level of use

Observation 1234 for C (4,3) = C (n, r)

1 2 3

1 2 4

1 3 4

2 3 4

For (a, b, c) type, the final first a = n-r + 1 is stopped when the traverse + 1 = 4-3 = 2, attention condition in line 47;

This limitation is that the number of combinations of "repeat" local conditions than full array difficult, firstly, for any combination of layer traversed does not exceed the first n-r + 1

If bool flag has been approached in the number of ideas is not difficult, and arrange almost, mark each iteration to skip the line, when returned at the end of the function can eliminate the mark

But if you find the law, we need to find a clear number can only be larger than the layer, and then based on this cycle to maximum n

 

The following is - dfs using Stack - n and the number of output code for the whole group of:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 typedef long long ll;
 9 using namespace std;
10 const int m=25;
11 ll a[m];
12 bool t=0;
13 ll sum=0;
14 int n;
15 ll k;
16  // standard number of combinations with output array !!!
 . 17  @ C (n-, K) = C (n-=. 1,. 1-K)
 18 is  
. 19  
20 is  // . 1. 5. 4. 3. 6 2
 21 is  // . 5 2 22 is 10. 6. 8. 4
 22 is  // . 5. 6. 8. 4 2 10 
23 is  LL B [m];
 24  // B [m] as a stack 
25  int G = . 1 ;
 26 is  LL R & lt;
 27  
28  void DFS ( int NUM, int IT) {
 29  
30      B [IT] = A [NUM];
 31 is      IF (IT == R & lt) {
 32          for ( int i=1; i<r; i++)printf("%lld ",b[i]);
33         printf("%lld\n",b[r]);
34         //cout<<g++<<" "<<it<<"\n";
35     } else {
36         for(int i=num+1; i<=n; i++) {
37             dfs(i,it+1);
38         }
39     }
40 }
41 
42 int main() {
43 
44     scanf("%d",&n);
45     for(int i=1; i<=n; i++)scanf("%lld",&a[i]);
46     for(r=1; r<=n; r++) {
47         for(int i=1; i<=n-r+1; i++) {
48             sum=0;
49             dfs(i,1);
50         }
51     }
52     puts("");
53 
54     return 0;
55 }
View Code

On this basis, directly to each sequence obtained by summing be completed this question,

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 typedef long long ll;
 9 using namespace std;
10 const int m=25;
11 ll a[m];
12 bool t=0;
13 ll sum=0;
14 int n;
15 ll k;
16  // standard number of combinations with output array !!!
 . 17  @ C (n-, K) = C (n-=. 1,. 1-K)
 18 is  
. 19  
20 is  // . 1. 5. 4. 3. 6 2
 21 is  // . 5 2 22 is 10. 6. 8. 4
 22 is  // . 5. 6. 8. 4 2 10 
23 is  LL B [m];
 24  // B [m] as a stack 
25  int G = . 1 ;
 26 is  LL R & lt;
 27  
28  
29  
30  void F ( int NO , int now) {
 31 is      IF (T == . 1 ) return ;
 32      B [now] = A [NO]; //更新入栈,
33     if(now==r) { //栈满检查
34         sum=0;
35         for(int i=1; i<=r&&t==0; i++) {
36             sum+=b[i];
37             if(sum==k)t=1;
38         }
39     } else {
40         for(int i=no+1; i<=n; i++) {
41             f(i,now+1);
42         }
43     }
44 }
45 
46 int main() {
47 
48     scanf("%d%lld",&n,&k);
49     for(int i=1; i<=n; i++)scanf("%lld",&a[i]);
50     for(r=1; r<=n; r++) {
51         for(int i=1; i<=n-r+1; i++) {
52             sum=0;
53             f(i,1);
54         }
55     }
56     if(t==1)cout<<"YE5";
57     else cout<<"N0";
58 
59     puts("");
60 
61 
62     return 0;
63 }
View Code

Of course, the summation, in fact, without the requirements of this sequence, traversing every possible, after the sequence does not need to survive, and direct examination, they meet up,

Free media approach the array (note that the first 44 rows , sum value Save back in the current operation to be executed in the end after each)

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 typedef long long ll;
 9 using namespace std;
10 const int m=25;
11 ll a[m];
12 bool t=0;
13 ll sum=0;
14 int n;
15 ll k;
16  // standard number of combinations with output array !!!
 . 17  @ C (n-, K) = C (n-=. 1,. 1-K)
 18 is  
. 19  
20 is  // . 1. 5. 4. 3. 6 2
 21 is  // . 5 2 22 is 10. 6. 8. 4
 22 is  // . 5. 6. 8. 4 2 10 
23 is  LL B [m];
 24  // B [m] as a stack 
25  int G = . 1 ;
 26 is  LL R & lt;
 27  
28  void F2 ( int NO, int now) {
 29      SUM = A + [NO]; // update stack,
 30      // COUT << "= SUM" SUM << << ""; 
31 is  
32      IF(SUM == T == K || . 1 ) {
 33 is          T = . 1 ;
 34 is          return ;
 35      }
 36      IF (now <R & lt) { // stack becomes full,
 37 [          // COUT << "= SUM" SUM << < < "\ n-";
 38 is          //
 39  
40          for ( int I = NO + . 1 ; I <= n-; I ++ ) {
 41 is              F2 (I, now + . 1 );
 42 is          }
 43 is      }
 44 is      sum- = A [NO]; / / pop less careful !!! this step 
45  }
 46  int main() {
47 
48     scanf("%d%lld",&n,&k);
49     for(int i=1; i<=n; i++)scanf("%lld",&a[i]);
50     
51     for(r=1; r<=n; r++) {
52         for(int i=1; i<=n-r+1; i++) {
53             sum=0;
54             f2(i,1);
55         }
56     }
57     if(t==1)cout<<"YE5";
58     else cout<<"N0";
59     puts("");
60     return 0;
61 }
View Code

In addition the wording - Source: https://blog.csdn.net/randyjiawenjie/article/details/6784355

Problem: Find the number n of combinations of K number, assuming the function prototype int combination (int n, int k), where n ranges ...... n. 1,

For example: combination (5,3) required output: 543,542,541,531,532,521,432,431,421,321

If the output array to be useful, it needs to dynamically allocate space for good at the beginning, at the end of the release. Or the use of recursion, critical: c (m, k) = c (m -1, k- 1) + c (m - 2, k - 1) + ... + c (k - 1.k - 1)

 

The include # <stdio.h> 
# MAXN DEFINE 100 
int A [MAXN];
 / * * 
 * combinatorial problem 
 * Problem description: Find a natural number 1, 2 ......, taken in all combinations of any m number k. 
 * / 
Void COMB ( int m, int K) {
     int I, J;
     for (I = m; I> = K; i-- ) { 
        A [K] = I;
         IF (K> . 1 ) 
            COMB (I - . 1 , K - . 1 );
         the else {
             for (J = A [ 0 ]; J> 0 ; J, )
            printf("%4d",a[j]);
            printf("\n");
        }
    }
}

int main() {
    a[0] = 3;
//    int a[] = {1,2,3,4,5};
    comb(5, 3);
    return 0;
}
View Code

Containing all - the output of the sequence - the subject requires a "stack" for the result, the difference that, he put on the boundary n-r + 1 written into the recursive function,

As expressed for (i = m; i> = k; i--), thinking more complex, each recursive time will change the upper bound,

Another way: is using the 01 notation, but the implementation has written a table + bfs achieve   https://blog.csdn.net/cao2219600/article/details/79587306

The same may be achieved using the 01 markers + dfs (Continued)

 

 

Guess you like

Origin www.cnblogs.com/KID-yln/p/12529283.html