Calculating stack distances efficiently
Introduction to Algorithms