Dynamic programming for some optimal control problems with hysteresis