### Problem Statement

SameGame is a game with a grid of colored blocks. If there are two or more blocks of the same color, all of which are connected, these form a group. More formally, a group is a set of 2 or more blocks, each of which has the same color, in which all pairs of blocks are connected by a sequence of horizontal or vertical steps, each of which moves to a block in the group. A group may be removed only if it is not a subset of some larger group. When a group is removed, all of the blocks above the removed blocks "fall down", meaning that any blocks that are immediately above an empty space move down until they are resting in the bottom row, or immediately above another block. If any entirely empty columns are created then columns to the right of the empty columns shift to the left until all of the empty columns are on the right side. As blocks are removed, points are accumulated based on the number of blocks removed each time. Each time a group of blocks is removed, the point total is increased by n*(n-1)/2 where n is the number of blocks in the group. Additionally, if every single block has been removed from the grid by the end of the game, the final score is multiplied by 4.

The input will be formatted as a String[], board, where each element represents a row of blocks, and each character tells which color a given block is.

For example, {"RBB","RRR","RBB"} represents:
```        RBB
RRR
RBB
```
Where 'R' and 'B' are the only two colors present.

One possible sequence of moves would be to first remove the five 'R's, which are all connected and form a group. This would give us 10 points (5*4/2) and make the board look as follows: (where '-' represents empty space):
```        -BB
---
-BB
```
Now the pieces fall down and shift to the left, giving:
```        ---
BB-
BB-
```
If we then remove the group of 4 B's, we get an additional 6 points. This removes all of the blocks so we multiply the total (16) by 4, and our final score is 64.

If the board is small enough, we can simply use brute force to find the optimal sequence of groups to remove to obtain the highest score. However, as the number of blocks grows, brute force quickly becomes infeasible. Thus, in order to write a program that plays well, we must use some intelligent technique to remove pieces - albeit a sub-optimal one. One important observation to make in trying to come up with a good strategy is that the number of points grows non-linearly, and we get more points per block if we remove larger groups. This suggests that we should save one color for last, and remove smaller groups first. Your task is to implement the following strategy:

You will be given a String, order, defining an ordering on the colors. You should always remove groups whose color appears earlier in order. So, if order were "RB", we would never remove a group of 'B's if there were a group of 'R's we could remove. When there are many groups of the same color, remove groups with less blocks in them first. If there is a tie, remove the group which extends into (has at least one character in) the lower indexed element of the board. If there is still a tie, remove the group which contains the lower indexed character in the lowest indexed element that both groups extend into. Using this method, continue removing groups until there are none left and then return the final score.

### Definition

 Class: SameGame Method: getScore Parameters: String[], String Returns: int Method signature: int getScore(String[] board, String order) (be sure your method is public)

### Constraints

-board will contain between 1 and 50 elements, inclusive.
-Each element of board will contain between 1 and 50 characters, inclusive.
-Each element of board will contain the same number of characters.
-Each character of each element of board will be an uppercase letter ('A'-'Z').
-order will contain between 1 and 26 uppercase letters, inclusive.
-No character will occur more than once in order.
-Each character in board will also be in order.

### Examples

0)

 ```{"RBB", "RRR", "RBB"}``` `"RB"`
`Returns: 64`
 This is the example from the problem statement.
1)

 ```{"ABCD", "ABCD", "ABCD", "ABCD"}``` `"ABCD"`
`Returns: 96`
 Here, we end up removing one column at a time, starting with the 'A's, and ending with the 'D's. After we remove each column, the remaining ones shift to the left. Each column gives us 4*3/2 points, for a total of 24 points. Since we can remove all of the blocks, we get a final score of 96.
2)

 `{"ABCD"}` `"ABCD"`
`Returns: 0`
3)

 ```{"ACCAA", "ABAAA", "ABBBA", "AAACD"}``` `"EABCD"`
`Returns: 28`
 First, we remove the 'L' shaped group of six 'A's, drop the blocks over empty space, and then shift left to get the following ('-' denotes empty space): ``` --AA- CCAA- BABA- BBCD- ``` Next, we remove the group of 5 'A's and end up with: ``` ----- CC--- BAB-- BBCD- ``` Finally, we remove the 3 'B's and get this board, which contains no groups: ``` ----- ----- -CB-- CACD- ```
4)

 ```{"ABABABABABABABABABAB", "ABABABABABABABABABAB", "CDCDCDCDCDCDCDCDCDCD", "CDCDCDCDCDCDCDCDCDCD", "EFEFEFEFEFEFEFEFEFEF", "EFEFEFEFEFEFEFEFEFEF", "GHGHGHGHGHGHGHGHGHGH", "GHGHGHGHGHGHGHGHGHGH", "IJIJIJIJIJIJIJIJIJIJ", "IJIJIJIJIJIJIJIJIJIJ", "KLKLKLKLKLKLKLKLKLKL", "KLKLKLKLKLKLKLKLKLKL", "MNMNMNMNMNMNMNMNMNMN", "MNMNMNMNMNMNMNMNMNMN", "OPOPOPOPOPOPOPOPOPOP", "OPOPOPOPOPOPOPOPOPOP", "QRQRQRQRQRQRQRQRQRQR", "QRQRQRQRQRQRQRQRQRQR", "STSTSTSTSTSTSTSTSTST", "STSTSTSTSTSTSTSTSTST", "UVUVUVUVUVUVUVUVUVUV", "UVUVUVUVUVUVUVUVUVUV", "WXWXWXWXWXWXWXWXWXWX", "WXWXWXWXWXWXWXWXWXWX", "YZYZYZYZYZYZYZYZYZYZ", "YZYZYZYZYZYZYZYZYZYZ", "WXWXWXWXWXWXWXWXWXWX", "WXWXWXWXWXWXWXWXWXWX", "UVUVUVUVUVUVUVUVUVUV", "UVUVUVUVUVUVUVUVUVUV", "STSTSTSTSTSTSTSTSTST", "STSTSTSTSTSTSTSTSTST", "MNMNMNMNMNMNMNMNMNMN", "MNMNMNMNMNMNMNMNMNMN", "QRQRQRQRQRQRQRQRQRQR", "QRQRQRQRQRQRQRQRQRQR", "OPOPOPOPOPOPOPOPOPOP", "OPOPOPOPOPOPOPOPOPOP", "KLKLKLKLKLKLKLKLKLKL", "KLKLKLKLKLKLKLKLKLKL", "IJIJIJIJIJIJIJIJIJIJ", "IJIJIJIJIJIJIJIJIJIJ", "GHGHGHGHGHGHGHGHGHGH", "GHGHGHGHGHGHGHGHGHGH", "EFEFEFEFEFEFEFEFEFEF", "EFEFEFEFEFEFEFEFEFEF", "CDCDCDCDCDCDCDCDCDCD", "CDCDCDCDCDCDCDCDCDCD", "ABABABABABABABABABAB", "ABABABABABABABABABAB"}``` `"ABCDEFGHIJKLMNOPQRSTUVWXYZ"`
`Returns: 2720`

#### Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=515

#### Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4703&pm=515

lbackstrom

#### Problem categories:

Recursion, Search