我怎样才能用贪心得到Om + n?

问题描述

在这种情况下,如何设计算法贪婪O(m+n)

给出两个安排L1L2,其中| L1 | = n| L2 | = m

如果可以从L2中删除元素以获得L1,则

L1L2的子序列。这意味着存在m个索引ik∈ [0..n],因此每个L1 [ik] = L2 [j]都有j∈ [0..m]。在O (n + m)中设计一个贪婪算法,以检测L2是否是L1的子序列,并在{{1}的情况下输出ik中的L1索引}是L2的子序列。

示例:

L1

我一直在用这个做经典的代码

Input L1 = [1 2 3 4 5 6 7 8 9 10] L2 = [3 5 7 9] Output ik = [2 4 6 8]

Input L1 = [2 1 4 3 6 5 7 8 9 10] L2 = [1 6 5 10] Output ik = [1 4 5 9]

Input L1 = [1 2 3 4 5 6 7 8 9 10] L2 = [1 5 9 12] Output L2 is not subsequence of L1

但是我不知道如何使用贪婪来获取bool isSubsequence(string str1,string str2) { int i=0; int j=0; while (j<str1.length() && i<str2.length()){ if(str1[j]==str2[i]){ cout<<i<<" "; j++; } i++; } return j==str1.length(); } 。 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...