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]

Note:

  1. 1 <= stamp.length <= target.length <= 1000
  2. stamp and target only contain lowercase letters.

这道题给了一个目标字符串 target,还有一个印戳字符串 stamp,现在有一个长度跟 target 一样的一排问号,每次可以在某个位置盖上印戳,新的印戳将会覆盖之前的字符,不论是问号还是其他字符,现在让找出所有盖印戳的位置,使得刚好可以盖出给定的字符串 target。这道题乍一看感觉还挺难下手的,毕竟一排问号,我们怎么知道该从哪里开始盖,但是如果换一个方向,假如给的是 target 字符串,每次盖印章,将对应的位置变成星号,只要将 target 中所有的字符盖成星号,最终再把盖印章的顺序翻转一下,就是题目中所求了。这里参考的是 [votrubac 大神的帖子](https://leetcode.com/problems/stamping-the-sequence/discuss/189576/C%2B%2B-simple-greedy),比如 target="aabccbc",stamp="abc",那么首先肯定是在 target 中找整个的 abc,可以找到,从位置1出开始盖,target 变为 a\*\*\*cbc,同时标记此时已经盖了3个字母,加入到 total 变量中。然后继续找 abc,没有的话,就需要改变印戳了,开始往里面加星号,首先加一个星号,加的位置有三个,分别是 ab\*, a\*c, \*bc,发现这三种都无法匹配,于是开始加两个星号,就有 a\*\*,\*\*c,其中 a\*\* 可以成功匹配,起始位置为0,total 加1,于是 target 变为 ****cbc,然后发现此时 \*\*c 也可以成功匹配,起始位置为2,total 加1,target 变为 *****bc。现在并不需要给 stamp 中加三个星号,这样没有意义,要做的是再从开头找一遍,此时发现 \*bc 可以匹配,起始位置为4,total 加2,到现在位置 target 完全变为星号,当无法进行盖印戳的时候,就退出循环,需要一个 isStamped 的变量来标记一下是否进行了戳印。循环退出后要将 res 数组翻转一下,同时还要看 total 是否等于 target 的长度,只有相等了,才说明每个字母都被戳印了,否则返回空集,参见代码如下:

class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
vector<int> res;
int n = stamp.size(), total = 0;
while (true) {
bool isStamped = false;
for (int size = n; size > 0; --size) {
for (int i = 0; i <= n - size; ++i) {
string t = string(i, '*') + stamp.substr(i, size) + string(n - size - i, '*');
auto pos = target.find(t);
while (pos != string::npos) {
res.push_back(pos);
isStamped = true;
total += size;
fill(begin(target) + pos, begin(target) + pos + n, '*');
pos = target.find(t);
}
}
}
if (!isStamped) break;
}
reverse(res.begin(), res.end());
return total == target.size() ? res : vector<int>();
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/936

参考资料:

https://leetcode.com/problems/stamping-the-sequence/

https://leetcode.com/problems/stamping-the-sequence/discuss/189576/C%2B%2B-simple-greedy

https://leetcode.com/problems/stamping-the-sequence/discuss/189258/C%2B%2B-Reverse-Operation-30-ms-better-than-DFS

https://leetcode.com/problems/stamping-the-sequence/discuss/450930/C%2B%2B-Reverse-the-stamping-process-%2B-Wildcard-matching

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 936. Stamping The Sequence 戳印序列的更多相关文章

  1. 936. Stamping The Sequence

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

  2. python3 第十七章 - sequence(序列)

    之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...

  3. EF中创建、使用Oracle数据库的Sequence(序列)功能

    ** 背景 ** 项目中订单号原来的生成规则由日期加随机数组成,后期需求决定将订单号生成规则更改为生成日期加当天当前订单数. 每天的订单数都是从0开始的,每生成一个订单,订单数就应该加1.订单数应该是 ...

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

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

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. 【LeetCode每天一题】Permutation Sequence(排列序列)

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

  8. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  9. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

随机推荐

  1. C++走向远洋——34(友元函数,成员函数和一般函数的区别)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:youyuan.cpp * 作者:常轩 * 微信公众号:Worl ...

  2. C++走向远洋——49(项目一2、复数类中的运算符重载、类的友元函数)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  3. mac 使用命令行向 github 提交代码

    让 mac 本地和自己的 github 网站建立连接(ssh) 下载安装 git 网址: https://git-scm.com/downloads 查看安装是否成功: git -version $ ...

  4. React拖拽组件Dragact V0.1.7:教你优化React组件性能与手感

    仓库地址:Dragact手感丝滑的拖拽布局组件 预览地址:支持手机端噢- 上回我们说到,Dragact组件已经进行了一系列的性能优化,然而面对大量数据的时候,依旧比较吃力,让我们来看看,优化之前的Dr ...

  5. 学习Java技术哪家强

    https://github.com/CyC2018/CS-Notes https://github.com/Snailclimb/JavaGuide SpringBoot 之 配置文件优先级 htt ...

  6. 老式车载导航如何支持大于4G的SD卡

    这个知识点以后会越来越没什么用,因为这类导航慢慢就会消失.记录这个,就是提醒自己如何防止以为很懂而被骗. 随着导航地图越来越大,4G的SD卡很快就不够用了,但是很不幸车载导航款式太老了,不支持大于4G ...

  7. Node的require和module.exports

    node编程中最重要的思想之一就是模块,在 Node.js 模块系统中,每个文件都被视为独立的模块.这是这个思想,让javascript的大规模工程成为可能.模块化编程在前端大肆盛行,在node中导出 ...

  8. 日常破解--XCTF easy_apk

    一.题目来源     来源:XCTF社区安卓题目easy_apk 二.破解思路     1.首先运行一下给的apk,发现就一个输入框和一个按钮,随便点击一下,发现弹出Toast验证失败.如下图所示: ...

  9. 使用Navicat Premiun远程连接MySQL失败,报错(10038)

    远程连接MySQL失败,可能有一下原因: 1.小伙子/小姑凉注意一下你的ip是否输入正确了!! 2.网络或防火墙问题 1).排查网络问题 使用命令:ping 192.168.1.1 查看网络请求是否超 ...

  10. Ubuntu部署Asp.net core网站无法访问

    前几天应工作需要,在阿里云上部署一个测试站点.本以为分分钟的事情,没想到打脸了. 当时直接新建一个webapi项目,publish后直接上传到阿里云,随后设置nginx转发网站端口5000. 接着打开 ...