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.

Aproach #1: C++.

class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
vector<int> ans;
vector<int> seen(target.length());
int total = 0;
while (total < target.length()) {
bool found = false;
for (int i = 0; i <= target.length() - stamp.length(); ++i) {
if (seen[i]) continue;
int l = unStamp(stamp, target, i);
if (l == 0) continue;
seen[i] = 1;
total += l;
ans.push_back(i);
found = true;
}
if (!found) return {};
}
reverse(begin(ans), end(ans));
return ans;
} private:
int unStamp(const string& stamp, string& target, int s) {
int l = stamp.length();
for (int i = 0; i < stamp.length(); ++i) {
if (target[s+i] == '*')
--l;
else if (target[s+i] != stamp[i])
return 0;
}
if (l != 0)
std::fill(begin(target)+s, begin(target)+s+stamp.length(), '*');
return l;
}
};

  

题意:

给出一个目标字符串和一个“印章”字符串,求出盖印章的位置,使得目标字符串被印章完全覆盖。若不能完全覆盖则返回空。

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

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

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

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

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

  3. LeetCode936. Stamping The Sequence

    一.题面 You want to form a target string of lowercase letters. At the beginning, your sequence is targe ...

  4. leetcode hard

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

  5. Swift LeetCode 目录 | Catalog

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

  6. hibernate 保存报错 Hibernate operation: could not get next sequence value;

    错误信息: [2017-09-28 18:51:38,854]-[org.hibernate.util.JDBCExceptionReporter:101] ERROR - Numeric Overf ...

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

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

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

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

  9. DG gap sequence修复一例

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

随机推荐

  1. bootstrap-select.js 下拉框多选后动态赋值

    话不多说先上demo 其实demo是从官网下载的 只稍作改动 由于没有搞清楚怎么上传源代码 就把官网的链接贴出来吧 https://github.com/silviomoreto/bootstrap- ...

  2. iOS 布局之 Springs and Struts

    “springs and struts” 模式,就是代码中的autosizing masks布局控制. autosizing mask决定了一个view会发生什么当它的superview 改变大小的时 ...

  3. 201671010140. 2016-2017-2 《Java程序设计》java学习第十一周

     java学习第十一周     本周,进行了java集合方面的知识,在博客园的帮助下,我的课前预习更有条理性,重点明确,本周的课堂反应明显更好了,首先,梳理一下本周知识点.  Collection   ...

  4. Linux实战教学笔记19:Linux相关网络知识梳理

    第十九节 Linux相关网络知识梳理 标签(空格分隔): Linux实战教学笔记-陈思齐 一,前言 一个运维有时也要和网络打交道,所以具备最基本的网络知识,对一个运维人员来说是必要的.但,对于我们的工 ...

  5. Excel VBA入门(六)过程和函数

    前面讲过,VBA代码有两种组织形式,一种就是过程(前面的示例中都在使用),另一种就是函数.其实过程和函数有很多相同之处,除了使用的关键字不同之外,还有不同的是: 函数有返回值,过程没有 函数可以在Ex ...

  6. 145. Binary Tree Postorder Traversal (Stack, Tree)

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. Mysql 使用触发器,把插入的数据在插入到宁一张表里

    CREATE TRIGGER tgr_tablea_insert AFTER //触发器名字 动作在插入数据之后 ON alertinfo //监听哪个表之后触发 FOR INSERT //监听的表的 ...

  8. ubuntu tensorflow cpu Faster-RCNN配置参考

    https://blog.csdn.net/qq_36652619/article/details/85006559     (参考) https://blog.csdn.net/zcy0xy/art ...

  9. Zedboard学习(二):zedboard的Linux下交叉编译环境搭建 标签: 交叉编译linuxzedboard 2017-07-04 23:49 19人阅读

    环境准备 首先肯定是要下载xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin文件,这是官方提供的linux下交叉编译链安装文件,下载地址为:https://p ...

  10. 使用dataview组件显示服务器端xml文件数据

    来自<sencha touch权威指南>,约193页开始 ------------------------------------- (1)app.js代码: Ext.require([' ...