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. kubernetes ServiceAccount

    api server 启动参数增加 –admission_control=…,ServiceAccount,…

  2. 结队编程第二次作业:Android自动生成算式应用

    一.题目要求 本次作业要求两个人合作完成,驾驶员和导航员角色自定,鼓励大家在工作期间角色随时互换,这里会布置两个题目,请各组成员根据自己的爱好任选一题. 这次我和我的小伙伴选择了题目一. 题目1: 实 ...

  3. 学习h5(开始)

    webstorm 下载地址:http://www.sdifenzhou.com/6176.html webstorm注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...

  4. TCP与UDP与HTTP协议

    http:是用于www浏览的一个协议.tcp:是机器之间建立连接用的到的一个协议. 1.TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.在网络层有IP协议.ICMP协议.ARP协议.R ...

  5. 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯

    [抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

  6. ECS 游戏架构 实现

    转载自:http://blog.csdn.net/i_dovelemon/article/details/27230719 实现 组件-实体-系统 - 博客频道   这篇文章是在我前面文章,理解组件- ...

  7. 谈谈Vue.js——vue-resource全攻略

    本篇文章主要介绍了谈谈Vue.js——vue-resource全攻略,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧   概述 上一篇我们介绍了如何将$.ajax和Vue. ...

  8. PCA 学习笔记

    先简单记下,等有时间再整理 PCA 主要思想,把 协方差矩阵 对角化,协方差矩阵是实对称的.里面涉及到矩阵论的一点基础知识: 基变换: Base2 = P · Base1 相应的 坐标变换 P · c ...

  9. poi 获取excel数据 导入数据库

    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map<String, ...

  10. Prolific PL2303 usb 转串口Win8 Win8.1驱动

    买了根USB转RS232串口的线,Pl2303芯片的.卖家和官方都称不支持Win8,但鄙人不信在Win7上能用在Win8/8.1就用不起来. 官方最新版的v1.9.0的驱动描述说不支持Win 8/8. ...