# Cut Ribbon Problem – Dynamic Programming Solutions

This is a C++ Program that Solves Cut the Ribbon Problem using Dynamic Programming technique.

Problem Description

Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:

After the cutting each ribbon piece should have length a, b or c.
After the cutting the number of ribbon pieces should be maximum.

Help Polycarpus and find the number of ribbon pieces after the required cutting.

Constraints-
1 <= n, a, b, c <= 4000 The numbers a, b and c can coincide.

Problem Solution

This seems like a greedy problem but actually is a DP problem. We will create an array dp[] to memoize values.

dp[i]=number of ribbon peices for n=i

We will initialize dp[0]=0 and then proceed in a bottom up fashion to tabulate the array.

Expected Input and Output

Case-1:

```
n=5
a=5
b=3
c=2

Expected result=2 (5=3+2)```

Case-2:

```
n=7
a=5
b=5
c=2

Expected result=2 (7=5+2)```

Case-3:

```
n=16
a=7
b=5
c=3

Expected result=4 (16=7+3+3+3)```
Program/Source Code

Here is source code of the C++ Program to solve Cut the Ribbon problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `//cut the ribbon`
2. `#include<bits/stdc++.h>`
3. `using namespace std;`
4. ` `
5. `int main()`
6. `{`
7. `    int i,j,k,n;`
8. `    int a,b,c;`
9. `    int x,y,z;`
10. ` `
11. `    cout<<"Enter the length of ribbon"<<endl;`
12. `    cin>>n;`
13. ` `
14. `    cout<<"Enter the 3 values of lengths allowed"<<endl;`
15. `    cin>>a>>b>>c;`
16. ` `
17. `    //array to memoize values`
18. `    vector<int> dp(n+1);`
19. ` `
20. `    //initialize`
21. `    dp[0]=0;`
22. ` `
23. `    for(i=1;i<=n;i++)`
24. `    {`
25. `        x=y=z=-1;`
26. ` `
27. `        if(i>=a)`
28. `            x=dp[i-a];`
29. ` `
30. `        if(i>=b)`
31. `            y=dp[i-b];`
32. ` `
33. `        if(i>=c)`
34. `            z=dp[i-c];`
35. ` `
36. `        if(x==-1 && y==-1 && z==-1)`
37. `            dp[i]=-1;`
38. ` `
39. `        else`
40. `            dp[i]=max(max(x,y),z)+1;`
41. `    }`
42. ` `
43. `    if(dp[n]==-1)`
44. `        cout<<"Not possible";`
45. ` `
46. `    else`
47. `        cout<<"Maximum number of pieces in which the ribbon can be cut is "<<endl<<dp[n];`
48. ` `
49. `    cout<<endl;`
50. ` `
51. `    return 0;`
52. `}`
Program Explanation

In the main function, we take input for n, a, b, c. Then, we create an array to fill it in bottom up manner. After tabulating the array, the result is displayed.

Runtime Test Cases
```
Case-1:
\$ g++ cut_the_ribbon.cpp
\$ ./a.out
Enter the length of ribbon
5
Enter the 3 values of lengths allowed
5 3 2
Maximum number of pieces in which the ribbon can be cut is
2

Case-2:
\$ ./a.out
Enter the length of ribbon
7
Enter the 3 values of lengths allowed
5 5 2
Maximum number of pieces in which the ribbon can be cut is
2

Case-3:
\$ ./a.out
Enter the length of ribbon
16
Enter the 3 values of lengths allowed
7 5 3
Maximum number of pieces in which the ribbon can be cut is
4```

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.