|
8 | 8 | */
|
9 | 9 | public class Dynamic_Programming implements Runnable {
|
10 | 10 |
|
11 |
| - BufferedReader c; |
12 |
| - PrintWriter pout; |
13 |
| - // static long mod = 1000000007; |
14 |
| - |
15 |
| - public void run() { |
16 |
| - try { |
17 |
| - c = new BufferedReader(new InputStreamReader(System.in)); |
18 |
| - pout = new PrintWriter(System.out, true); |
19 |
| - solve(); |
20 |
| - pout.close(); |
21 |
| - } catch (Exception e) { |
22 |
| - pout.close(); |
23 |
| - e.printStackTrace(); |
24 |
| - System.exit(1); |
25 |
| - } |
26 |
| - } |
27 |
| - |
28 |
| - public static void main(String[] args) throws Exception { |
29 |
| - new Thread(new Dynamic_Programming()).start(); |
30 |
| - } |
31 |
| - |
32 |
| - void solve() throws Exception { |
33 |
| - //longest_increasing_subsequence(); |
34 |
| - //longest_common_subsequence(); |
35 |
| - //edit_distance(); |
36 |
| - min_cost_path(); |
37 |
| - } |
38 |
| - |
39 |
| - void min_cost_path(){ |
40 |
| - // Read more : |
41 |
| - // http://www.geeksforgeeks.org/dynamic-programming-set-6-min-cost-path/ |
42 |
| - long cost[][] = { {1, 2, 3}, |
43 |
| - {4, 8, 2}, |
44 |
| - {1, 5, 3} }; |
45 |
| - pout.println(min_cost_dp(cost, 2, 2)); |
46 |
| - |
47 |
| - } |
48 |
| - |
49 |
| - long min_cost_dp(long cost[][],int m,int n){ |
50 |
| - long dp[][] = new long [cost.length][cost[0].length]; |
51 |
| - dp[0][0] = cost[0][0]; |
52 |
| - |
53 |
| - for(int i=1;i<=m;i++){ |
54 |
| - dp[i][0] = dp[i-1][0] + cost[i][0]; |
55 |
| - } |
56 |
| - |
57 |
| - for(int j=1;j<=n;j++){ |
58 |
| - dp[0][j] = dp[0][j-1] + cost[0][j]; |
59 |
| - } |
60 |
| - |
61 |
| - for(int i=1;i<=m;i++){ |
62 |
| - for(int j=1;j<=n;j++){ |
63 |
| - dp[i][j] = cost[i][j] + Math.min(Math.min(dp[i-1][j-1], dp[i][j-1]), dp[i-1][j]); |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 |
| - return dp[m][n]; |
68 |
| - } |
69 |
| - |
70 |
| - void edit_distance(){ |
71 |
| - String str1 = "unday"; |
72 |
| - String str2 = "monday"; |
73 |
| - |
74 |
| - pout.println(edit_distance_dp(str1,str2)); |
75 |
| - } |
76 |
| - |
77 |
| - long edit_distance_dp(String s1,String s2){ |
78 |
| - int len1 = s1.length(); |
79 |
| - int len2 = s2.length(); |
80 |
| - |
81 |
| - long dp[][] = new long[len1+1][len2+1]; |
82 |
| - |
83 |
| - for(int i=0;i<=len1;i++){ |
84 |
| - for(int j=0;j<=len2;j++){ |
85 |
| - if(i==0){ |
86 |
| - dp[i][j] = j; |
87 |
| - continue; |
88 |
| - } |
89 |
| - |
90 |
| - if(j==0){ |
91 |
| - dp[i][j] = i; |
92 |
| - continue; |
93 |
| - } |
94 |
| - |
95 |
| - if(s1.charAt(i-1)==s2.charAt(j-1)){ |
96 |
| - dp[i][j] = dp[i-1][j-1]; |
97 |
| - } |
98 |
| - else{ |
99 |
| - dp[i][j] = 1 + Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]); |
100 |
| - } |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - return dp[len1][len2]; |
105 |
| - } |
106 |
| - |
107 |
| - private void longest_common_subsequence() { |
108 |
| - String x = "AGGTAB"; |
109 |
| - String y = "AGTB"; |
110 |
| - |
111 |
| - pout.println("LCS: "+lcs(x, y)); |
112 |
| - } |
113 |
| - |
114 |
| - int lcs(String input1,String input2){ |
115 |
| - int len1 = input1.length(); |
116 |
| - int len2 = input2.length(); |
117 |
| - int dp[][] = new int[len1+1][len2+1]; |
118 |
| - for(int i=1;i<=len1;i++){ |
119 |
| - for(int j=1;j<=len2;j++){ |
120 |
| - if(input1.charAt(i-1)==input2.charAt(j-1)){ |
121 |
| - dp[i][j] = dp[i-1][j-1] + 1; |
122 |
| - } |
123 |
| - else |
124 |
| - dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); |
125 |
| - } |
126 |
| - } |
127 |
| - |
128 |
| - return dp[len1][len2]; |
129 |
| - } |
130 |
| - |
131 |
| - private void longest_increasing_subsequence() { |
132 |
| - int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }; |
133 |
| - int n = arr.length; |
134 |
| - pout.println("Length of lis is " |
135 |
| - + lis(arr, n) + "\n"); |
136 |
| - } |
137 |
| - |
138 |
| - int lis(int arr[],int n){ |
139 |
| - int lis[] = new int[n]; |
140 |
| - int i,j,max = 0; |
141 |
| - |
142 |
| - for (i=0;i<n;i++) |
143 |
| - lis[i] = 1; |
144 |
| - |
145 |
| - for(i=1;i<n;i++){ |
146 |
| - for(j=0;j<i;j++){ |
147 |
| - if(arr[i]>arr[j]&&lis[i]<lis[j]+1) |
148 |
| - lis[i] = lis[j]+1; |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - for(i=0;i<n;i++) |
153 |
| - if(max<lis[i]) |
154 |
| - max=lis[i]; |
155 |
| - |
156 |
| - return max; |
157 |
| - } |
158 |
| - |
159 |
| -} |
| 11 | + BufferedReader c; |
| 12 | + PrintWriter pout; |
| 13 | + // static long mod = 1000000007; |
| 14 | + |
| 15 | + public void run() { |
| 16 | + try { |
| 17 | + c = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + pout = new PrintWriter(System.out, true); |
| 19 | + solve(); |
| 20 | + pout.close(); |
| 21 | + } catch (Exception e) { |
| 22 | + pout.close(); |
| 23 | + e.printStackTrace(); |
| 24 | + System.exit(1); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + new Thread(new Dynamic_Programming()).start(); |
| 30 | + } |
| 31 | + |
| 32 | + void solve() throws Exception { |
| 33 | + // longest_increasing_subsequence(); |
| 34 | + // longest_common_subsequence(); |
| 35 | + // edit_distance(); |
| 36 | + // min_cost_path(); |
| 37 | + coin_change(); |
| 38 | + } |
| 39 | + |
| 40 | + void coin_change() { |
| 41 | + // Given a value N, if we want to make change for |
| 42 | + // N cents, and we have infinite supply of each of |
| 43 | + // S = { S1, S2, .. , Sm} valued coins, how many ways |
| 44 | + // can we make the change? The order of coins doesn’t |
| 45 | + // matter. |
| 46 | + // |
| 47 | + // For example, for N = 4 and S = {1,2,3}, |
| 48 | + // there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. |
| 49 | + // So output should be 4. |
| 50 | + // For N = 10 and S = {2, 5, 3, 6}, there are five solutions: |
| 51 | + // {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. |
| 52 | + // So the output should be 5 |
| 53 | + |
| 54 | + // refer geekforgeek for more info |
| 55 | + // http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/ |
| 56 | + int arr[] = { 1, 2, 3 }; |
| 57 | + pout.println(coin_change_rec(arr, arr.length, 4)); |
| 58 | + } |
| 59 | + |
| 60 | + int coin_change_rec(int arr[], int len, int sum) { |
| 61 | + if (sum == 0) |
| 62 | + return 1; |
| 63 | + |
| 64 | + if (sum < 0) |
| 65 | + return 0; |
| 66 | + |
| 67 | + if (len <= 0 && sum >= 1) |
| 68 | + return 0; |
| 69 | + |
| 70 | + return coin_change_rec(arr, len - 1, sum) + coin_change_rec(arr, len, sum - arr[len - 1]); |
| 71 | + } |
| 72 | + |
| 73 | + void min_cost_path() { |
| 74 | + // Read more : |
| 75 | + // http://www.geeksforgeeks.org/dynamic-programming-set-6-min-cost-path/ |
| 76 | + long cost[][] = { { 1, 2, 3 }, { 4, 8, 2 }, { 1, 5, 3 } }; |
| 77 | + pout.println(min_cost_dp(cost, 2, 2)); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + long min_cost_dp(long cost[][], int m, int n) { |
| 82 | + long dp[][] = new long[cost.length][cost[0].length]; |
| 83 | + dp[0][0] = cost[0][0]; |
| 84 | + |
| 85 | + for (int i = 1; i <= m; i++) { |
| 86 | + dp[i][0] = dp[i - 1][0] + cost[i][0]; |
| 87 | + } |
| 88 | + |
| 89 | + for (int j = 1; j <= n; j++) { |
| 90 | + dp[0][j] = dp[0][j - 1] + cost[0][j]; |
| 91 | + } |
| 92 | + |
| 93 | + for (int i = 1; i <= m; i++) { |
| 94 | + for (int j = 1; j <= n; j++) { |
| 95 | + dp[i][j] = cost[i][j] + Math.min(Math.min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return dp[m][n]; |
| 100 | + } |
| 101 | + |
| 102 | + void edit_distance() { |
| 103 | + String str1 = "unday"; |
| 104 | + String str2 = "monday"; |
160 | 105 |
|
| 106 | + pout.println(edit_distance_dp(str1, str2)); |
| 107 | + } |
161 | 108 |
|
| 109 | + long edit_distance_dp(String s1, String s2) { |
| 110 | + int len1 = s1.length(); |
| 111 | + int len2 = s2.length(); |
| 112 | + |
| 113 | + long dp[][] = new long[len1 + 1][len2 + 1]; |
| 114 | + |
| 115 | + for (int i = 0; i <= len1; i++) { |
| 116 | + for (int j = 0; j <= len2; j++) { |
| 117 | + if (i == 0) { |
| 118 | + dp[i][j] = j; |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + if (j == 0) { |
| 123 | + dp[i][j] = i; |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if (s1.charAt(i - 1) == s2.charAt(j - 1)) { |
| 128 | + dp[i][j] = dp[i - 1][j - 1]; |
| 129 | + } else { |
| 130 | + dp[i][j] = 1 + Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return dp[len1][len2]; |
| 136 | + } |
| 137 | + |
| 138 | + private void longest_common_subsequence() { |
| 139 | + String x = "AGGTAB"; |
| 140 | + String y = "AGTB"; |
| 141 | + |
| 142 | + pout.println("LCS: " + lcs(x, y)); |
| 143 | + } |
| 144 | + |
| 145 | + int lcs(String input1, String input2) { |
| 146 | + int len1 = input1.length(); |
| 147 | + int len2 = input2.length(); |
| 148 | + int dp[][] = new int[len1 + 1][len2 + 1]; |
| 149 | + for (int i = 1; i <= len1; i++) { |
| 150 | + for (int j = 1; j <= len2; j++) { |
| 151 | + if (input1.charAt(i - 1) == input2.charAt(j - 1)) { |
| 152 | + dp[i][j] = dp[i - 1][j - 1] + 1; |
| 153 | + } else |
| 154 | + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return dp[len1][len2]; |
| 159 | + } |
| 160 | + |
| 161 | + private void longest_increasing_subsequence() { |
| 162 | + int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }; |
| 163 | + int n = arr.length; |
| 164 | + pout.println("Length of lis is " + lis(arr, n) + "\n"); |
| 165 | + } |
| 166 | + |
| 167 | + int lis(int arr[], int n) { |
| 168 | + int lis[] = new int[n]; |
| 169 | + int i, j, max = 0; |
| 170 | + |
| 171 | + for (i = 0; i < n; i++) |
| 172 | + lis[i] = 1; |
| 173 | + |
| 174 | + for (i = 1; i < n; i++) { |
| 175 | + for (j = 0; j < i; j++) { |
| 176 | + if (arr[i] > arr[j] && lis[i] < lis[j] + 1) |
| 177 | + lis[i] = lis[j] + 1; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + for (i = 0; i < n; i++) |
| 182 | + if (max < lis[i]) |
| 183 | + max = lis[i]; |
| 184 | + |
| 185 | + return max; |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments