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. iOS学习之UIPickerView控件的简单使用

    UIPickerView控件在给用户选择某些特定的数据时经常使用到,这里演示一个简单的选择数据,显示在UITextField输入框里,把UIPickerView作为输入View,用Toolbar作为选 ...

  2. 【原】Coursera—Andrew Ng机器学习—Week 11 习题—Photo OCR

    [1]机器学习管道 [2]滑动窗口 Answer:C ((200-20)/4)2 = 2025 [3]人工数据 [4]标记数据 Answer:B (10000-1000)*10 /(8*60*60) ...

  3. 在Yii Framework中利用PHPMailer发送邮件(2011-06-02 14:06:23)

    转载▼ 标签: it 分类: 技术共享 官方扩展链接:http://www.yiiframework.com/extension/mailer/这个扩展配置十分方便,如果有问题的话,可以打开Debug ...

  4. 蓝桥杯练习系统算法训练习题加答案java版本

    附上百度文库的链接:http://wenku.baidu.com/view/afb78d36b42acfc789eb172ded630b1c59ee9bf7

  5. java求最长公共子串的长度

    1这道题目就是给定两个字符串,然后求这两个字符串的最长公共子串的最大长度,假设我的f()方法是来求两个字符串的最大公共子串,从头开始逐一比较,如果相等,则 继续调用这个方法,使得递归的长度+1,如果不 ...

  6. 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】

    题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...

  7. 【总结整理】AI产品经理大会2017(转载)

    从企业大数据到企业 AI | 易观智慧院院长 李智 1.AI 不是目的,而是要了解 AI 是什么,真正意义上的强人工智能在前沿领域尚未取得突破,暂时只能在影视文学作品中去思考人机关系.机器人三定律在未 ...

  8. 如何用shell脚本执行或关闭jar包服务?

    现在springboot很流行,但是大家都知道springboot是以jar包的方式进行打包的,那样就少不了开启或关闭服务的操作了,但是命令方式未免过于繁琐. 下面记录shell脚本的方式启动或关闭服 ...

  9. securecrt免密码登录

    一.前言 1. 环境 客户端系统:win7 securecrt版本:6.0.2 服务端系统:centos6.5 服务端ssh实现:openssh 2. 关于认证方式 我们知道ssh一般都会提供多种客户 ...

  10. Linux常见问题及解决方案

    问题一: 删除Linux 的烦恼(没出现系统选择菜单只出现"grub": 问题描述: 安装了Linux.WinXP双系统,采用Grub引导系统.在XP下通过格式化磁盘(非法操作)删 ...