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

  

题意:

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

最新文章

  1. OpenLayers.2.10.Beginners.Guide---第一章
  2. wcf第4步之原生调用简单封装
  3. Mono addin 学习笔记 4 再论数据扩展点(Data only extension point)
  4. AOJ-2249 Road Construction(最短路)
  5. Android版的疯狂猜图游戏源码完整版分享
  6. HTTPS访问:weblogic下配置SSL
  7. [Tutorial] Using the RasPi as a WiFi hostspot
  8. Ubuntu下安装ADT(图文教程)
  9. GCD浅析
  10. SqlParameter参数类型为int32时候的传值陷阱
  11. idea—— 模版配置
  12. CountDownLatch的实现原理
  13. Havel-Hakimi定理---通过度数列判断是否可图化
  14. .net core2.0通过entityframework访问Sqlserver数据库
  15. 用PS做圆角图片
  16. HTML(二)选择器
  17. Redis笔记(2)单机数据库实现
  18. C#复制数据到剪切板
  19. Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server..
  20. JQUERY中的事件处理:RETURN FALSE、阻止默认行为、阻止冒泡以及兼容性问题

热门文章

  1. ios常用空间UIScrollViewIndicator的一些属性
  2. pip / conda 导出和安装环境组件 requirements.txt
  3. java 蓝桥杯算法提高 _1区间k大数查询
  4. Vuex笔记/axios笔记
  5. 微信小程序开发教程,大多数人都搞错的八个问题
  6. 17. Merge Two Binary Trees 融合二叉树
  7. Boost线程详解
  8. p4364 [九省联考2018]IIIDX
  9. openpose 问题
  10. u盘安装Linux系统详细教程