首页 > 其他分享 >936. Stamping The Sequence

936. Stamping The Sequence

时间:2022-08-22 07:22:39浏览次数:51  
标签:index Stamping target Sequence stamp get length abc 936

You are given two strings stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'.

In one turn, you can place stamp over s and replace every letter in the s with the corresponding letter from stamp.

  • For example, if stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can:
    • place stamp at index 0 of s to obtain "abc??",
    • place stamp at index 1 of s to obtain "?abc?", or
    • place stamp at index 2 of s to obtain "??abc".
    Note that stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot place stamp at index 3 of s).

We want to convert s to target using at most 10 * target.length turns.

Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain target from s within 10 * target.length turns, return an empty array.

 

Example 1:

Input: stamp = "abc", target = "ababc"
Output: [0,2]
Explanation: Initially s = "?????".
- Place stamp at index 0 to get "abc??".
- Place stamp at index 2 to get "ababc".
[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]
Explanation: Initially s = "???????".
- Place stamp at index 3 to get "???abca".
- Place stamp at index 0 to get "abcabca".
- Place stamp at index 1 to get "aabcaca".

 

Constraints:

  • 1 <= stamp.length <= target.length <= 1000
  • stamp and target consist of lowercase English letters.
class Solution {
    public int[] movesToStamp(String stamp, String target) {
        char[] s = stamp.toCharArray();
        char[] t = target.toCharArray();
        int sl = s.length;
        int tl = t.length;
        int star = 0;
        List<Integer> pos = new ArrayList();
        boolean[] visited = new boolean[tl];
        
        while(star < tl) {
            boolean replaced = false;
            for(int i = 0; i <= tl - sl; i++) {
                if(!visited[i] && canreplace(s, t, i)) {
                    star = doreplace(t, i, s);
                    visited[i] = true;
                    pos.add(i);
                    replaced = true;
                    if(star == tl) break;
                }
            }
            if(!replaced) return new int[0];
        }
        
        int[] res = new int[pos.size()];
        for(int i = 0; i < pos.size(); i++) {
            res[i] = pos.get(pos.size() - i - 1);
        }
        return res;
    }
    
    public boolean canreplace(char[] s, char[] t, int p) {
        for(int i = 0; i < s.length; i++) {
            if(t[i + p] != '*' && t[i + p] != s[i]) return false;
        }
        return true;
    }
    
    public int doreplace(char[] t, int p, char[] s) {
        int c = 0;
        for(int i = 0; i < s.length; i++) {
            if(t[i + p] != '*') t[i + p] = '*';
        }
        for(char ch : t) {
            if(ch == '*') c++;
        }
        return c;
    }  
}

https://leetcode.com/problems/stamping-the-sequence/discuss/201546/12ms-Java-Solution-Beats-100

Thinking reversely

标签:index,Stamping,target,Sequence,stamp,get,length,abc,936
From: https://www.cnblogs.com/wentiliangkaihua/p/16611617.html

相关文章

  • CF815 D2 Xor-Subsequence (hard version)(01trie)
    传送门sb题面误导了我半天。按位考虑,对于\(a[i]\)和\(i\)的一位考虑什么样的\(a[j]\)和\(j\)可以转移过来,发现这一位有一种一定可以一种一定不行,还有两种不确定。考虑......
  • "蔚来杯"2022牛客暑期多校训练营2 G-Link with Monotonic Subsequence
    问题描述First,let'sreviewsomedefinitions.Feelfreetoskipthispartifyouarefamiliarwiththem.Asequence aaaisanincreasing(decreasing)subsequ......
  • Codeforces Round #815 (Div. 2) D2 Xor-Subsequence (hard version)
    原题链接\(A>B\),总是有二进制下从高到低的前\(k\)位相等,第\(k+1\)位\(A\)是\(1\),\(B\)是\(0\)本题中\(A=a_i\oplusj\),\(B=a_j\oplusi\),这里有一个很奇妙的性质(手玩或者......
  • D2. Xor-Subsequence (hard version)
    D2.Xor-Subsequence(hardversion)昨天cf的E题,挺好的一个DP优化问题。暴力的DP就是设dp[i]表示以i结尾的最长长度。转移时枚举之前的所有j,复杂度O(n^2)。考虑怎么优......
  • longest increasing subsequence
    300. LongestIncreasingSubsequenceMediumGivenanintegerarray nums,returnthelengthofthelongeststrictlyincreasingsubsequence.A subsequence......
  • 【Azure 事件中心】从Azure Event Hub中消费数据,如何查看当前消费客户端消费数据的Off
    问题描述当通过AzureEventHubSDK消费EventHub中的消息时,必须指定一个StorageAccount(存储账号)用于保存Checkpoint(检查点)。 比如在C#代码中,需要指定StorageAc......
  • CF145C Lucky Subsequence
    题目链接:洛谷CodeforcesProblem这题目翻译真的神了,好多歧义,看不懂,给一个本人翻译:给你一个长度为\(n\)的序列\(a\),定义幸运数为仅含有\(4\)或\(7\)的数,你需要取......
  • 《GB25936.3-2012》PDF下载
    《GB25936.3-2012橡胶塑料粉碎机械第3部分:切碎机安全要求》PDF下载《GB25936.3-2012》PDF下载GB25936的本部分规定了橡胶塑料用切碎机设计和制造所适用的安全要求;该......
  • 《GB25936.2-2012》PDF下载
    《GB25936.2-2012橡胶塑料粉碎机械第2部分:拉条式切粒机安全要求》PDF下载《GB25936.2-2012》简介GB25936的本部分规定了加工橡胶塑料用拉条式切粒机设计和制造的基本......
  • 《GB25936.1-2012》PDF下载
    《GB25936.1-2012橡胶塑料粉碎机械第1部分:刀片式破碎机安全要求》PDF下载《GB25936.1-2012》简介本标准规定了破碎橡胶塑料制品或材料用刀片式破碎机的设计和制造安......