Connect
Time limit5sMemory limit512 MB
Place each string in its row in order across the C columns so that the number of adjacent equal-letter cell pairs is maximized.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, String
- Solved
- No attempts yet
Problem
Connect is a solitaire puzzle that uses several letter tiles.
The table has empty cells. For each , you must put a string in the -th row of the table. The letter order cannot change. In other words, choose an integer sequence with , and put the -th character of in column .
For example, when and is "ICPC", you can place it like this:
I_C_P_C_
ICPC____
_IC___PC
'_' stands for an empty cell.
For each non-empty cell , you get one point for every adjacent cell that has the same character as . Two cells are adjacent if they share an edge. Find the maximum total point you can get.
Input
The first line contains two integers and . Then lines follow, each containing a string . All characters of are uppercase letters.
Output
Print the maximum total point in one line.