The Dow Jones and the S&P 500 gained on Thursday as investors flocked to cyclical stocks expected to benefit from a strengthening economy, while shifting out of technology shares.