一、题面

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;
}
};

  

LeetCode936. Stamping The Sequence的更多相关文章

  1. [Swift]LeetCode936. 戳印序列 | Stamping The Sequence

    You want to form a target string of lowercase letters. At the beginning, your sequence is target.len ...

  2. 936. Stamping The Sequence

    You want to form a target string of lowercase letters. At the beginning, your sequence is target.len ...

  3. [LeetCode] 936. Stamping The Sequence 戳印序列

    You want to form a `target` string of lowercase letters. At the beginning, your sequence is target.l ...

  4. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  5. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  6. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  7. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  8. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  9. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. java简单的测试方法执行了多少时间

    (1)以毫秒为单位的 long startTime = System.currentTimeMillis(); // 获取开始时间 // doThing(); // 测试的代码段 long endTi ...

  2. CentOS双网卡双IP设置

    CentOS双网卡双IP设置 系统环境:CentOS Linux 网络环境: 两个IP地址,192.168.0.10和10.10.30.2,掩码是255.255.255.0,这两个子网的网关地址分别是 ...

  3. c语言实践:RS信号报告

    题目: 无线电台的RS制信号报告是由三两个部分组成的: R(Readability) 信号可辨度即清晰度. S(Strength)    信号强度即大小. 其中R位于报告第一位,共分5级,用1—5数字 ...

  4. export default {} 和new Vue()区别

     1.export default 的用法:相当于提供一个接口给外界,让其他文件通过 import 来引入使用. 而对于export default 和export的区别:  在JavaScript ...

  5. 在aspx页面中使用三元表达式

    第一种使用方法:判断GridView绑定的数据是否为空 用GridView或其他控件绑定数据的时候,有时候需要判断从数据库中获取的值是否是空值,然后显示相应的内容,如果在后置代码中写的话只有是在Row ...

  6. dev初识 拖动分组

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm ...

  7. 专题2-通过按键玩中断\2440按键中断编程lesson2

    1.程序优化 修改Makefile 把main.c里面的mmu代码复制到mmu.c并修改如下 main.c的修改 由于在bootloader当中一般不会使用MMU,所以 main.c 加入led.c文 ...

  8. 为docker设置国内镜像

    docker的默认镜像(https://hub.docker.com/)地址,拉取镜像时是比较慢的,经常会超时,有时拉取几个小时.为了加快拉取的时间和速度,需要添加中国的镜像地址: 国内的加速地址: ...

  9. Eclipse下Android的NDK开发环境配置

    编辑2016年7月26日——增加了下载网址,修改了一些错误. 摸索了一周,走了很多弯路,磕磕绊绊,总算是弄好了NDK的开发环境,在这里总结一下吧. 一.Android NDK开发环境 首先下载安装JR ...

  10. 手机打车APP的机遇与挑战

    所谓打车APP,就是个能安装在手机上的打车软件.原理是通过GPS进行定位,能够搜索附近的空车信息然后反馈给用户.同样的,空车信息也会反馈给用户.一般这种啊APP都是跟地图类软件一起的.比如百度地图,谷 ...