leetcodeFreedom trail

Posted syb3181

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcodeFreedom trail相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    int findRotateSteps(string ring, string key) {
        int M = ring.size();
        int N = key.size();
        auto f = vector<vector<int>>(M,vector<int>(N + 1,-1));
        f[0][0] = 0;
        
        for (int j = 0;j<N;j++) {
            for (int i = 0;i<M;i++) {
            if (f[i][j] == -1) continue;
            char target = key[j];
            
            for (int k = -M;k <= 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<M;i++) {
            if (f[i][N] != -1)
            if (ret == -1 || ret > f[i][N]) ret = f[i][N];
        }
        return ret;
    }
};

差5分钟,马丹

以上是关于leetcodeFreedom trail的主要内容,如果未能解决你的问题,请参考以下文章

LightOJ 1282 Leading and Trailing (数学)

E - Leading and Trailing

ES8(2017)尾逗号 Trailing commas

git rebase 解决 trailing whitespace errors

Count the consecutive zero bits (trailing) on the right with multiply and lookup

C. Trailing Loves (or L'oeufs?) (质因数分解)