题目描述
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 <= stamp.length <= target.length <= 1000
stamp
andtarget
only contain lowercase letters.
题意:你想要用小写字母组成一个目标字符串 target。 开始的时候,序列由 target.length 个 ‘?’ 记号组成。而你有一个小写字母印章 stamp。在每个回合,你可以将印章放在序列上,并将序列中的每个字母替换为印章上的相应字母。你最多可以进行 10 * target.length 个回合。
举个例子,如果初始序列为 “?????”,而你的印章 stamp 是 “abc”,那么在第一回合,你可以得到 “abc??”、”?abc?”、”??abc”。(请注意,印章必须完全包含在序列的边界内才能盖下去。)
如果可以印出序列,那么返回一个数组,该数组由每个回合中被印下的最左边字母的索引组成。如果不能印出序列,就返回一个空数组。
例如,如果序列是 “ababc”,印章是 “abc”,那么我们就可以返回与操作 “?????” -> “abc??” -> “ababc” 相对应的答案 [0, 2];另外,如果可以印出序列,那么需要保证可以在 10 * target.length 个回合内完成。任何超过此数字的答案将不被接受。
算法1
(贪心/反向考虑)
题解:这道题正向考虑比较复杂,但是如果我们考虑如何将target变成?????
问题似乎变得可以解决了。首先第一次我们盖章的位置必须和stamp完全匹配,接下来我们将匹配的区间字符全部替换成?
,在下一次匹配中,?
可以匹配任意字符。接下来我们就利用上述思路不断的将target变成??????
,如果有一轮中我们没有办法匹配而且当前target也还没有被匹配完就说明无法匹配。如下是一个合法的匹配过程,然后我们将答案逆序输出即可。
同时我们可以知道同一个位置不可能盖两次,因为第一次盖完这个区间已经是?
了,再盖一次没有意义,所以我们使用一个标记数组来判断当前位置是否被盖过。
aabcaca
a????ca
a??????
???????
C++ 代码
class Solution {
public:
int n,m,total;
vector<int> movesToStamp(string stamp, string target) {
n = target.length(),m = stamp.length(),total = 0;
vector<int> res,seen(n,0);
while(total < n)
{
bool flag = false;
for(int i = 0 ; i <= n - m ;i ++)
{
if(seen[i] == 1) continue;
int l = compare(stamp,target,i);
if(l == 0) continue;
seen[i] = 1;
total += l;
res.push_back(i);
flag = true;
}
// 当前这轮没有任何一个区间可以匹配,直接返回
if(flag == false) break;
}
reverse(res.begin(),res.end());
return total == n?res:vector<int> {};
}
// 查看当前区间是否匹配,如果匹配,匹配了多少个非?的字符,并且把target这个区间全部置为?
int compare(string &stamp,string& target,int k)
{
int l = m;
for(int i = 0 ;i < m; i ++)
{
if(target[i + k] == '?')
l --;
else if(target[i + k] != stamp[i])
return 0;
}
if(l != 0)
{
for(int i = 0 ;i < m ; i ++)
target[i + k] = '?';
}
return l;
}
};