Finding Patterns in Time Series: A Dynamic Programming Approach