class Solution {public: int findRotateSteps(string ring, string key) { int M = ring.size(); int N = key.size(); auto f = vector>(M,vector (N + 1,-1)); f[0][0] = 0; for (int j = 0;j <= M;k++) { int index = i + k; if (index < 0) index += M; index = index % M; if (ring[index] == target) { if (f[index][j + 1] == -1 || f[index][j + 1] > f[i][j] + 1 + fabs(k) ) { f[index][j + 1] = f[i][j] + 1 + fabs(k); } } } } } int ret = -1; for (int i = 0;i f[i][N]) ret = f[i][N]; } return ret; }};
差5分钟,马丹