514. Freedom Trail (Hard)

原题目:

   In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to open the door.

  Given a string ring, which represents the code engraved on the outer ring and another string key, which represents the keyword needs to be spelled. You need to find the minimum number of steps in order to spell all the characters in the keyword.
Initially, the first character of the ring is aligned at 12:00 direction. You need to spell all the characters in the string key one by one by rotating the ring clockwise or anticlockwise to make each character of the string key aligned at 12:00 direction and then by pressing the center button.
At the stage of rotating the ring to spell the key character key[i]:

  1. You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step. The final purpose of the rotation is to align one of the string ring’s characters at the 12:00 direction, where this character must equal to the character key[i].
  2. If the character key[i] has been aligned at the 12:00 direction, you need to press the center button to spell, which also counts as 1 step. After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you’ve finished all the spelling.

Example:
这里写图片描述

  Input: ring = “godding”, key = “gd”
  Output: 4
Explanation:
  For the first key character ‘g’, since it is already in place, we just need 1 step to spell this character.
  For the second key character ‘d’, we need to rotate the ring “godding” anticlockwise by two steps to make it become “ddinggo”.
  Also, we need 1 more step for spelling.
  So the final output is 4.

Note:

1.Length of both ring and key will be in range 1 to 100.
2.There are only lowercase letters in both strings and might be some duplcate characters in both strings.
3.It's guaranteed that string key could always be spelled by rotating the string ring.

题目大意如下:

  给定一个转盘,转盘上有一个名为ring的字符串,再给定一个名为key的关键字符串,转盘中间有一个button。要求按照key转动ring使得key里面的字符按顺序出现在转盘的12点方向,每转动到下一个字符(可以向左也可以向右)算一步,转动到对应字符后按下button表示确定,这也算一步(每个key的字符对应完后都要按一下button)。
  
注意:

ring和key的长度不超过100。
字符串中只包含小写字母。
输入确保可以利用ring得到key。

解题思路:
  首先,我们遵循一个公式:(i为key中第一个字符)
       ans = min_steps[i][i+1] + min_steps[i+1][i+2] + min_steps[i+2][i+3] + …..
  
  拿样例来说:
       ans = min_steps[g][d]

  这道题最棘手的地方在于字符串可以向左转也可以向右转,并且每一次转完之后都要记录下在12点钟方向的字符。但同时,解决这道题的思路也变得很直接明白——动态规划。我们假设dp[i][j]表示我们为key中的第i个字符匹配到ring中的第j个字符需要的步数,当然匹配成功条件是ring[j] == key[i]。然后我们通过在匹配过程中,把一个一个的步数加起来就行了,就像我们开头提到的公式。

  当i == 0 时,代表这是key中第一个数,所以他并没有之前的结果,所以直接用
       min(dp[i][j] , min(abs(i - j),ring_size - abs(i - j)))
就可以得到在ring中找到key[i]的最小步数。

  当i != 0 时,我们需要找到key[i]上一个字符(即key(i - 1))在ring中的位置以便确定dp[i][j],在此过程中我们还要把它们加起来,所以最终结果就是dp数组当中最后一排中(即key[key.size() - 1]这一行)最小的那个数字。
  
动态规划(dp)代码如下:

class Solution {
public:
    int findRotateSteps(string ring, string key) {
        int key_size = key.size() , ring_size = ring.size() ;
        int ans = INT_MAX ;

        //create a dp matrix to store data
        vector<vector<int> > dp(key_size , vector<int>(ring_size , INT_MAX) ) ; 

        //we are looking up key[i] in ring
        for(int i = 0 ; i < key_size ; ++i){  
            for(int j = 0 ; j < ring_size ; ++j){
                //if we have found a match
                if(key[i] == ring[j]){  
                    //when i == 0,it means the first char in key and it has no previous reslut
                    if(i == 0)  
                        //min(abs(i - j),ring_size - abs(i - j)) means we can turn left and right 
                        //but only take the shortest one
                        dp[i][j] = min(dp[i][j] , min(abs(i - j),ring_size - abs(i - j))) ;
                    else
                    //when i != 0,it has previous reslut,we have to find the last char in ring
                    //so that we can sum the steps and store it in dp[i][j]
                        for(int k = 0 ; k < ring_size ; ++k){
                            if(dp[i - 1][k] != INT_MAX){
                                dp[i][j] = min(dp[i][j] , dp[i - 1][k]+min(abs(k - j),ring_size - abs(k - j))) ;
                                //the answer is the smallest number in the last row
                                if(i == key_size - 1 && dp[i][j] < ans) ans = dp[i][j] ;
                            }
                        }
                }
            }
        }

        return ans + key_size ;
    }
};

运行结果:
运行结果

算法分析:
  有三个循环所以很明显是O(n³)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值