Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 删除子字符串中所有引用的测试用例_String_Algorithm_String Matching - Fatal编程技术网

String 删除子字符串中所有引用的测试用例

String 删除子字符串中所有引用的测试用例,string,algorithm,string-matching,String,Algorithm,String Matching,上周,我为我的学院举办了一次黑客大会,如果给了一个字符串,我们需要删除子字符串的第一次出现或最后一次出现 示例s=“acaac”和t=“a” s是主字符串t是子字符串 s可以是“caaac”或“acaac”,我们需要找到给定s和t的最大移动次数 输入仅包含小写字母[a-z] 测试用例1: s=“aabb”和t=“ab”,删除“aabb”s中出现的t变为“ab”,然后删除字符串的唯一出现项以获取s=“” 由于在t的s中不再出现,我们返回2 测试用例2: s=“aabcd”t=“abc”->只发生一

上周,我为我的学院举办了一次黑客大会,如果给了一个字符串,我们需要删除子字符串的第一次出现或最后一次出现

示例s=“acaac”和t=“a” s是主字符串t是子字符串

s可以是“caaac”或“acaac”,我们需要找到给定s和t的最大移动次数

输入仅包含小写字母[a-z]

测试用例1: s=“aabb”和t=“ab”,删除“aabb”s中出现的t变为“ab”,然后删除字符串的唯一出现项以获取s=“” 由于在t的s中不再出现,我们返回2

测试用例2: s=“aabcd”t=“abc”->只发生一次,因此计数为1

测试用例3: s=“aa”t=“b”计数=0

我在java中尝试了以下伪代码

 count = 0   
 while(s.contains(t))
 {
 s=s.replacefirst(t,"")
 count++;
 }
 return count;
但是我没有得到我遗漏的测试用例,我在我的项目中14个测试用例中通过了9个


我能知道我遗漏了哪些测试用例吗?

下面是一个示例,您的代码将给出错误的答案:

s=ababaa,t=aba

您将删除第一个引用,将导致:

(aba)baa->baa->1

但是,如果先删除第二个引用,则可以删除一个附加子字符串:

ab(aba)a->aba->''->2

似乎您必须迭代所有可能的第一次/最后一次删除组合,并选择最佳结果


更有趣的问题是,是否有比暴力更好的算法?

下面是一个示例,您的代码将给出错误的答案:

s=ababaa,t=aba

您将删除第一个引用,将导致:

(aba)baa->baa->1

但是,如果先删除第二个引用,则可以删除一个附加子字符串:

ab(aba)a->aba->''->2

似乎您必须迭代所有可能的第一次/最后一次删除组合,并选择最佳结果


更有趣的问题是,是否有比暴力更好的算法?

这可以通过使用BFS轻松解决,我不认为我们可以比暴力做得更好,也许我们可以在将字符串添加到队列时,添加忽略移动小于
maxMove
s的字符串的优化

以下代码给出了上述所有输入的正确输出。我使用了
JUnit.Assert.assertEquals()

import java.util.Deque;
导入java.util.LinkedList;
导入静态org.junit.Assert.assertEquals;
公共类再投资{
静态类移动{
字符串str;
int移动;
公共移动(字符串str、int移动){
this.str=str;
this.moves=移动;
}
}
静止的
公共整数maxMoves(字符串s、字符串t){
int max=0;
如果(s==null | | t==null | | t.length()>s.length())
返回最大值;
Deque q=新链接列表();
q、 提议(新举措(s,0));
而(!q.isEmpty()){
对于(int sz=q.size();sz>0;--sz){
Moves curMove=q.poll();
max=Math.max(max,curMove.moves);
if(curMove.str.contains(t)){
int-nextStart=curMove.str.indexOf(t,0);
如果(nextStart!=-1){
q、 提供(新移动(curMove.str.substring(0,nextStart)+curMove.str.substring(nextStart+t.length()),curMove.Moves+1));
}
int lastStart=curMove.str.lastIndexOf(t,curMove.str.length());
如果(lastStart!=-1){
q、 提供(新移动(curMove.str.substring(0,lastStart)+curMove.str.substring(lastStart+t.length()),curMove.Moves+1));
}
}
}
}
返回最大值;
}
公共静态void main(字符串[]args){
资产质量(3,maxMoves(“babba”,“b”));
资产质量(2,maxMoves(“aabb”、“ab”);
资产质量(2,maxMoves(“ababaa”、“aba”);
assertEquals(1,maxMoves(“abaer”,“er”);//应该是1
assertEquals(1,maxMoves(“erababa”,“er”);//应该是1
assertEquals(1,maxMoves(“zzzeraba”,“er”);//应该是1
assertEquals(2,maxMoves(“aerbabaer”,“er”);//应该是2
assertEquals(2,maxMoves(“aeerra”,“er”);//应该是2
assertEquals(2,maxMoves(“aeera”,“er”);//应该是2
assertEquals(2,maxMoves(“aerbera”,“er”);//应该是2
assertEquals(1,maxMoves(“aberebraba”,“er”);//应该是1
}
}

使用BFS可以很容易地解决这个问题,我不认为我们能比暴力做得更好,也许我们可以在将字符串添加到队列时,添加忽略移动量小于
maxMove
s的字符串的优化

以下代码给出了上述所有输入的正确输出。我使用了
JUnit.Assert.assertEquals()

import java.util.Deque;
导入java.util.LinkedList;
导入静态org.junit.Assert.assertEquals;
公共类再投资{
静态类移动{
字符串str;
int移动;
公共移动(字符串str、int移动){
this.str=str;
this.moves=移动;
}
}
静止的
公共整数maxMoves(字符串s、字符串t){
int max=0;
如果(s==null | | t==null | | t.length()>s.length())
返回最大值;
Deque q=新链接列表();
q、 提议(新举措(s,0));
而(!q.isEmpty()){
对于(int sz=q.size();sz>0;--sz){
Moves curMove=q.poll();
max=Math.max(max,curMove.moves);
if(curMove.str.contains(t)){
int-nextStart=curMove.str.indexOf(t,0);
如果(nextStart!=-1){
q、 提供(新移动(curMove.str.substring(0,nextStart)+curMove.str.substring(nex
import java.util.Deque;
import java.util.LinkedList;
import static org.junit.Assert.assertEquals;

public class RemoveString {
    static class Moves {
        String str ;
        int moves ;
        public Moves(String str, int moves) {
            this.str = str ;
            this.moves = moves ;
        }
    }

    static
    public int maxMoves(String s, String t) {
        int max = 0 ;
        if (s == null || t == null || t.length() > s.length())
            return max ;
        Deque<Moves> q = new LinkedList<>() ;
        q.offer(new Moves(s, 0)) ;
        while (!q.isEmpty()) {
            for (int sz = q.size() ;  sz > 0 ; -- sz) {
                Moves curMove = q.poll() ;
                max = Math.max(max, curMove.moves) ;
                if (curMove.str.contains(t)) {
                    int nextStart = curMove.str.indexOf(t, 0) ;
                    if (nextStart != -1) {
                        q.offer(new Moves(curMove.str.substring(0, nextStart) + curMove.str.substring(nextStart+t.length()), curMove.moves+1)) ;
                    }
                    int lastStart = curMove.str.lastIndexOf(t, curMove.str.length()) ;
                    if (lastStart != -1) {
                        q.offer(new Moves(curMove.str.substring(0, lastStart) + curMove.str.substring(lastStart+t.length()), curMove.moves+1)) ;
                    }
                }
            }
        }
        return max ;
    }

    public static void main(String[] args) {
        assertEquals(3, maxMoves("babba", "b"));
        assertEquals(2, maxMoves("aabb", "ab"));
        assertEquals(2, maxMoves("ababaa", "aba"));
        assertEquals(1, maxMoves("abaer", "er")); // Should be 1
        assertEquals(1, maxMoves("erababa", "er")); // Should be 1
        assertEquals(1, maxMoves("zzzeraba", "er")); // Should be 1
        assertEquals(2, maxMoves("aerbabaer", "er")); // Should be 2
        assertEquals(2, maxMoves("aeerra", "er")); // Should be 2
        assertEquals(2, maxMoves("aerera", "er")); // Should be 2
        assertEquals(2, maxMoves("aerbera", "er")); //Should be 2
        assertEquals(1, maxMoves("aberebraba", "er")); // Should be 1
    }
}