Apr 15, 2007 00:01
Horrible TCO on my part. It was one of those nights where I knew exactly what to do, but the programming was absolutely wrong.
I had many errors, and it ultimately led me to my downfall, but see if you can spot the error with the following code (assume all the logic is correct, the error might or might not be subtle, but it's totally context-less.) Yes, this is the "standard" textbook N^3 left-right DP.
ArrayList[,] cache = new ArrayList[51,51];
bool[,] done = new bool[51,51];
public ArrayList go( int left, int right ) {
if ( done[left,right] ) return cache[left,right];
int[] c = new int[26];
for ( int i = left; i <= right; i++ )
c[ s[i] - 'a' ]++;
bool d = true;
for ( int i = 0; i < 26; i++ ) if ( c[i] > 1 ) d = false;
if ( d ) {
ArrayList ret = new ArrayList();
ret.Add( s.Substring( left, right - left + 1 ) );
return ret;
}
ArrayList best = null;
for ( int i = left; i < right; i++ ) {
// split on between i, i, i + 1, right
ArrayList now = go( left, i );
ArrayList now2 = go( i + 1, right );
for ( int k = 0; k < now2.Count; k++ ) now.Add( now2[k] );
now.Sort();
if ( best == null ) best = now;
if ( better( now, best ) ) {
best = new ArrayList();
for ( int k = 0; k < now.Count; k++ ) best.Add( now[k] );
}
}
done[left,right] = true;
cache[left,right] = best;
return best;
}
Better lucky than good - and apparently I wasn't either!