514 Freedom Trail 自由之路

Posted lina2014

tags:

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

详见:https://leetcode.com/problems/freedom-trail/description/

C++:

class Solution {
public:
    int findRotateSteps(string ring, string key) 
    {
        int n = ring.size(), m = key.size();
        vector<vector<int>> dp(m + 1, vector<int>(n));
        for (int i = m - 1; i >= 0; --i)
        {
            for (int j = 0; j < n; ++j) 
            {
                dp[i][j] = INT_MAX;
                for (int k = 0; k < n; ++k)
                {
                    if (ring[k] == key[i])
                    {
                        int diff = abs(j - k);
                        int step = min(diff, n - diff);
                        dp[i][j] = min(dp[i][j], step + dp[i + 1][k]);
                    }
                }
            }
        }
        return dp[0][0] + m;      
    }
};

 参考:http://www.cnblogs.com/grandyang/p/6675879.html

以上是关于514 Freedom Trail 自由之路的主要内容,如果未能解决你的问题,请参考以下文章

java 514. Freedom Trail.java

java 514. Freedom Trail.java

java 514. Freedom Trail.java

java 514. Freedom Trail.java

java 514. Freedom Trail.java

java 514. Freedom Trail.java