Efficient algorithms for shape and pattern matching