LeetCode936. Stamping The Sequence

本文探讨了一种解决特定字符串匹配问题的逆向思维策略,通过从目标字符串的末尾开始寻找并记录盖章操作,最终反转操作序列得到解决方案。详细解析了算法步骤,并提供了C++代码实现。

一、题面

You want to form a target string of lowercase letters.

At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters.

On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns.

For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)

If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array.

For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc".

Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.

 

Example 1:

Input: stamp = "abc", target = "ababc"
Output: [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)

Example 2:

Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]

二、分析

该题一定要把它替换的过程多画几遍,然后结合题意,因为题意说,每次盖的印章都是完整的盖下去的,那么你相当于每次都要在target这个串里面找到长度为stamp.length的位置给stamp盖,如果直接思考,会发现不好实现。

所以我们逆向想一下,因为我后盖的章会覆盖掉前面的章,那么我前面的章无论什么时候盖对后盖的章都是没有影响的,所以我先找到最后一次章盖的位置,即在target序列中找stamp,记下盖的序列的最左边位置,然后把相应位置全部替换为‘?’,相当于就是例2中的

"a????ca",然后把这个?看成一个万能的字母,它可以为任意的,再找stamp,这样,一直到最后全部变成target,这些序列就是从最后一次到第一次盖章的位置,然后倒转一下就是题目所要求的。需要注意的是,在写代码的时候,一定要注意需要判断匹配的字符串可能全是?,所以需要防止这种情况产生。

三、代码

class Solution {
public:
    vector<int> movesToStamp(string stamp, string target) {
        vector<int> ans, output;
        string aim(target.length(), '?');
        while(target != aim)
        {
            int t = moveAstamp(stamp, target);
            if(t == -1)
            {
                return {};
            }
            ans.push_back(t);
        }
        for(int i = ans.size()-1; i >= 0; i--)
            output.push_back(ans[i]);
        return output;
    }
    
    int moveAstamp(string stamp, string &target){
        
        for(int i = 0; i < target.length(); i++)
        {
            int j = 0, k = i;
            bool flag = false;
            while(j < stamp.length() && k < target.length() && (stamp[j] == target[k] || target[k] == '?'))
            {
                if(stamp[j] == target[k])
                    flag = true;
                j++;
                k++;
            }
            if(j == stamp.length() && flag)
            {
                for(int t = i; t < k; t++)
                {
                    target[t] = '?';
                }
                return i;
            }
        }
        return -1;
    }
};

  

转载于:https://www.cnblogs.com/dybala21/p/9929176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值