@@ -146,6 +146,111 @@ score for the rest, then add in the score for the current choice.
146146
147147Return the max of all the choices.
148148
149+
150+ ```
151+ Result align(String S, String T) {
152+ if (S.length() == 0 && T.length() == 0) {
153+ return Result(0);
154+ } else if (S.length() == 0) {
155+ rest = align(S, T.substring(0, T.length()-1));
156+ return Result(rest.score - 1, Direction.LEFT)
157+ } else if (T.length() == 0) {
158+ rest = align(S.substring(0, S.length()-1), T);
159+ return Result(rest.score - 1, Direction.UP)
160+ } else {
161+ char s = S.charAt(S.length() - 1);
162+ char t = T.charAt(T.length() - 1);
163+ // option 1: line up
164+ rest1 = align(S.substring(0, S.length()-1),
165+ T.substring(0, T.length()-1));
166+ option1 = rest1.score + match(s, t);
167+ best_score = option1;
168+ best_direction = DIAGONAL;
169+ // option 2: gap on top (S)
170+ rest2 = align(S, T.substring(0, T.length()-1));
171+ option2 = rest2.score - 1;
172+ if (option2 > best_score) {
173+ best_score = option2;
174+ best_direction = LEFT;
175+ }
176+ // option 3: gap on bottom (T)
177+ rest3 = align(S.substring(0, S.length()-1), T);
178+ option3 = rest3.score - 1;
179+ if (option3 > best_score) {
180+ best_score = option3;
181+ best_direction = UP;
182+ }
183+ return Result(best_score, best_direction);
184+ }
185+ }
186+ ```
187+
188+ Memoize
189+ ```
190+ void put_2D(HashMap<String, HashMap<String, Result> > R,
191+ String S, String T) {
192+ if (R.contains(S)) {
193+ R.get(S).put(T, result);
194+ } else {
195+ R.put(S, new HashMap<String,Result>());
196+ R.get(S).put(T, result);
197+ }
198+ }
199+
200+
201+ Result align(String S, String T,
202+ HashMap<String, HashMap<String, Result> > R)
203+ {
204+ if (R.contains(S) and R.get(S).contains(T)) {
205+ return R.get(S).get(T);
206+ }
207+
208+ if (S.length() == 0 && T.length() == 0) {
209+ result = Result(0);
210+ put_2D(R, S, T);
211+ return result;
212+ } else if (S.length() == 0) {
213+ rest = align(S, T.substring(0, T.length()-1), R)
214+ result = Result(rest.score - 1, Direction.LEFT);
215+ put_2D(R, S, T);
216+ return result;
217+ } else if (T.length() == 0) {
218+ rest = align(S.substring(0, S.length()-1), T, R);
219+ result = Result(rest.score - 1, Direction.UP)
220+ put_2D(R, S, T);
221+ return result;
222+ } else {
223+ char s = S.charAt(S.length() - 1);
224+ char t = T.charAt(T.length() - 1);
225+ // option 1: line up
226+ rest1 = align(S.substring(0, S.length()-1),
227+ T.substring(0, T.length()-1), R);
228+ option1 = rest1.score + match(s, t);
229+ best_score = option1;
230+ best_direction = DIAGONAL;
231+ // option 2: gap on top (S)
232+ rest2 = align(S, T.substring(0, T.length()-1), R);
233+ option2 = rest2.score - 1;
234+ if (option2 > best_score) {
235+ best_score = option2;
236+ best_direction = LEFT;
237+ }
238+ // option 3: gap on bottom (T)
239+ rest3 = align(S.substring(0, S.length()-1), T, R);
240+ option3 = rest3.score - 1;
241+ if (option3 > best_score) {
242+ best_score = option3;
243+ best_direction = UP;
244+ }
245+ result = Result(best_score, best_direction);
246+ put_2D(R, S, T);
247+ return result;
248+ }
249+ }
250+ ```
251+
252+
253+
149254## Subproblem identification
150255
151256Instead of using the entire rest of S and T as the inputs to the
0 commit comments