Hello, I’m d000hg, and this is my blog. I’m going to use it to talk about anything I find interesting. That means, in nearly all cases, something related to programming or software development. As well as that, articles and algorithmic problems I write will appear here.
As well as here, you can find me on www.gamedev.net and www.topcoder.com/tc
So for this first post, here is a problem I wrote, in a style similar to those found on TopCoder, USACO, etc. Feel free to post ideas, code, bugs, … If you want to discuss this problem on TopCoder’s forums I’m fine with that, we discuss USACO, SPOJ there after all, but please link back to this post.
I have a fair few problems stored up so expect them every couple of days…
I’m new to blogging so any tips about the way I write, or how my blog is set up, are welcome.
2D Palindromes.
A palindrome is a sequence of letters which is the same no matter which way it is read. For instance HANNAH or ABCBA. A 2-dimensional palindrome extends this idea for a table of letters. A section of a table is 2D-palindromic if each row is a palindrome and each column is also a palindrome. For instance:
ABCDEDCBA
BCDEFEDCB
ABCDEDCBA
Here each row is a 9-letter palindrome, and each row a 3-letter palindrome.
Given a string[] grid, where the ith element is the ith row of a character grid, return the area of the largest 2-D palindromic region within this grid.
Examples:
“ABCDEDCBA”,
“BCDEFEDCB”,
“ABCDEDCBA” The whole region is a 2D palindromic so return 9×3 = 27.
“AAAAAAAAAAAAA”,
“AAAAAAAAABBBB”,
“AAAAAAAAABBBB”,
“AAAAAAAAABBBB” There is a 9×4 palindromic region consisting entirely of ‘A’ characters.
Notes:
grid may have between 1 and 50 entries; each entry may have between 1 and 50 upper-case characters A-Z; all entries in grid are the same length.
November 28, 2007 at 3:35 pm |
First!
November 28, 2007 at 3:38 pm |
I believe it can be solved in O(n^2 + n^2)
dp1[k][i][j] = true if on line k from position i to position j there is a palindrome, false otherwise
Also while computing this compute : a[i][j] = set of lengths of horizontal palindroms from position i, j
dp2[k][i][j] = the longest width that we can have given that we select from column k, the rows from i to row j.
Meaning that we selected the height of the area, (j-i), the column k, our rectangular are will start at row i, and column k (the top left corner), and we compute the maximum length that the rectangle will have. (using the a array )
Thus it could be solved in O(grid.length^2 * grid.height + grid.height^2 * grid.length)
aprox : O(n^3) …
I have the semestrial math exam tomorrow, busy with last minute study, but i’ll try an implementation on Friday.
Nice problem, Thx.
November 28, 2007 at 3:38 pm |
Ignore the first line, gibberish, sorry.