-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpoker-chips.cpp
148 lines (123 loc) · 3.41 KB
/
poker-chips.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @file poker-chips.cpp
* @author prakash ([email protected])
* @brief
Suppose you are given n poker chips stacked in two stacks, where the edges
of all chips can be seen. Each chip is one of three colors. A turn consists of
choosing a color and removing all chips of that color from the tops of the
stacks. The goal is to minimize the number of turns until the chips are gone.
For example, consider the stacks (RRGG, GBBB). Playing red, green, and
then blue suffices to clear the stacks in three moves. Give an O(n2) dynamic
programming algorithm to find the best strategy for a given pair of chip piles.
* @version 0.1
* @date 2021-08-28
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
#include <string>
#define MAXLEN 101 /* longest possible string */
/* [[[ editdistance_mid_cut */
#define MATCH 0 /* enumerated type symbol for match */
#define INSERT 1 /* enumerated type symbol for insert */
#define DELETE 2 /* enumerated type symbol for delete */
/* [[[ editdistance_cell_struct_cut */
typedef struct {
int cost; /* cost of reaching this cell */
int parent; /* parent cell */
} cell;
/* ]]] */
/* For normal edit distance computation */
cell m[MAXLEN + 1][MAXLEN + 1];
void goal_cell(std::string s, std::string t, int *i, int *j) {
*i = s.length() - 1;
*j = t.length() - 1;
}
int match(char c, char d) {
if (c == d) {
return (0);
}
return (1);
}
int indel(char c) { return (1); }
void row_init(int i, cell m[MAXLEN + 1][MAXLEN + 1]) {
m[0][i].cost = i;
if (i > 0) {
m[0][i].parent = INSERT;
} else {
m[0][i].parent = -1;
}
}
void column_init(int i, cell m[MAXLEN + 1][MAXLEN + 1]) {
m[i][0].cost = i;
if (i > 0) {
m[i][0].parent = DELETE;
} else {
m[0][i].parent = -1;
}
}
void match_out(std::string s, std::string t, int i, int j) {
if (s[i] == t[j]) {
printf("M");
} else {
printf("S ");
std::cout<< s[i] << " " ;
}
}
void insert_out(std::string t, int j) { printf("I"); }
void delete_out(std::string s, int i) { printf("D"); }
/* [[[ reconstruct_path_ed_cut */
void reconstruct_path(std::string s, std::string t, int i, int j,
cell m[MAXLEN + 1][MAXLEN + 1]) {
if (m[i][j].parent == -1) {
return;
}
if (m[i][j].parent == MATCH) {
reconstruct_path(s, t, i - 1, j - 1, m);
match_out(s, t, i, j);
return;
}
if (m[i][j].parent == INSERT) {
reconstruct_path(s, t, i, j - 1, m);
insert_out(t, j);
return;
}
if (m[i][j].parent == DELETE) {
reconstruct_path(s, t, i - 1, j, m);
delete_out(s, i);
return;
}
}
using namespace std;
void bestStrategy(string S1, string S2) {
int i, j, k;
int opt[3];
for (i = 0; i <= MAXLEN; i++) {
row_init(i, m);
column_init(i, m);
}
for (i = 1; i < S1.length(); i++) {
for (j = 1; j < S1.length(); j++) {
opt[MATCH] = m[i - 1][j - 1].cost + match(S1[i], S2[j]);
opt[INSERT] = m[i][j - 1].cost + indel(S2[j]);
opt[DELETE] = m[i - 1][j].cost + indel(S1[i]);
m[i][j].cost = opt[MATCH];
m[i][j].parent = MATCH;
for (k = INSERT; k <= DELETE; k++) {
if (opt[k] < m[i][j].cost) {
m[i][j].cost = opt[k];
m[i][j].parent = k;
}
}
}
}
goal_cell(S1, S2, &i, &j);
cout << (m[i][j].cost) << endl;
reconstruct_path(S1, S2, i, j, m);
}
int main(int argc, const char **argv) {
string S1 = "RRGG", S2 = "GBBB";
bestStrategy(S1, S2);
return 0;
}