将字符串A所需附加的最小子序列以获得字符串B
在这个问题中,我们需要使用str1的子序列来构造str2。为了解决这个问题,我们可以找到str1的子序列,使其能够覆盖最大长度为str2的子串。在这里,我们将学习两种不同的方法来解决问题。
问题陈述 – 我们给出了两个不同长度的字符串:str1 和 str2。我们需要按照以下条件从 str1 构造 str2。
从 str1 中选取任何子序列,并将其附加到新字符串(最初为空)。
我们需要返回构造str2所需的最少操作数,如果无法构造str2,则打印-1。
示例
输入– str1 =“acd”,str2 =“adc”
输出– 2
说明
str1 中的第一个子序列是“ad”。所以,我们的字符串可以是“ad”。
之后,我们可以从 str1 中获取“c”子序列并将其附加到“ad”以使其成为“adc”。
输入– str1 = "adcb", str2 = "abdca"
输出– 3
说明
第一个子序列是 str1 中的“ab”。
之后,我们可以获取“dc”字符串,结果字符串将是“abdc”
接下来,我们可以使用“a”子序列来生成最终的字符串“abdca”。
方法 1
在这种方法中,我们将迭代 str1 以查找多个子序列并将其附加到结果字符串中。
算法
定义长度为 26 的“arr”数组,并将所有元素初始化为 0,以存储字符在 str1 中的存在。
迭代str1,根据字符的ASCII值更新数组元素的值
定义“last”变量并使用 -1 进行初始化以跟踪最后访问的元素。另外,定义“cnt”变量并将其初始化为 0 以存储操作计数。
开始使用循环遍历 str2。
如果当前字符不在str1中,则返回-1。
使用“last + 1”值初始化“j”变量。
使用while循环进行迭代,直到j的值小于len,且str1[j]不等于字符
如果‘j’的值大于‘len’,我们遍历‘str1’。增加 'cnt' 变量的值,将 'last' 初始化为 -1,因为我们需要再次遍历 'str1',将 'I' 的值减少 1,因为我们需要再次考虑当前字符,使用 ' continue' 关键字继续迭代。
将“last”变量的值更新为“j”。
循环的所有迭代完成后返回“cnt + 1”。这里,我们需要将“cnt”添加“1”,因为我们不考虑最后一个操作。
示例
#include <iostream> using namespace std; // function to count the minimum number of operations required to get string str2 from subsequences of string str1. int minOperations(string str1, string str2){ int len = str1.length(); // creating an array of size 26 to store the presence of characters in string str1. int arr[26] = {0}; // storing the presence of characters in string str1. for (int i = 0; i < len; i++){ arr[str1[i] - 'a']++; } // store the last iterated index of string str1. int last = -1; // to store the count of operations. int cnt = 0; for (int i = 0; i < str2.length(); i++){ char ch = str2[i]; // if the character is not present in string str1, then return -1. if (arr[ch - 'a'] == 0){ return -1; } // start iterating from the jth index of string str1 to find the character ch. int j = last + 1; while (j < len && str1[j] != ch){ j++; } // if j is equal to the length of string str1, then increment the count, set last to -1, and decrement i. if (j >= len){ cnt++; last = -1; --i; continue; } // set last to j. last = j; } // return cnt + 1 as we haven't counted the last operation. return cnt + 1; } int main(){ string str1 = "acd", str2 = "adc"; int operations = minOperations(str1, str2); cout << "Minimum number of operations required to create string B from the subsequences of the string A is: " << operations << "\n"; return 0; }
输出
Minimum number of operations required to create string B from the subsequences of the string A is: 2
时间复杂度 – O(N*M),其中 N 是 str2 的长度,M 是 str1 的长度。
空间复杂度 - O(1),因为我们不使用任何动态空间。
方法2
在这种方法中,我们将使用映射和集合数据结构来提高上述方法的效率。解决问题的逻辑与上面的方法相同。
算法
定义“chars_mp”以将 char -> 集{}存储为键值对。
在映射中,存储 str1 字符串中存在特定字符的索引集
定义“last”和“cnt”变量
开始遍历str2。如果包含当前字符索引的集合的大小为零,则返回 -1。
查找当前字符索引集中“last”的上限。
如果找不到上限,请将“cnt”的值增加 1,将“last”设置为 -1,将“I”的值减少 1,然后使用 continue 关键字。 p>
更新“last”变量的值。
循环迭代完成后,返回‘cnt’变量的值
示例
#include <iostream> #include <map> #include <set> using namespace std; // function to count the minimum number of operations required to get string str2 from subsequences of string str1. int minOperations(string str1, string str2){ // Length of string str1 int len = str1.length(); // creating the map to store the set of indices for each character in str1 map<char, set<int>> chars_mp; // Iterate over the characters of str1 and store the indices of each character in the map for (int i = 0; i < len; i++){ chars_mp[str1[i]].insert(i); } // store the last visited index of str1 int last = -1; // Stores the required count int cnt = 1; // Iterate over the characters of str2 for (int i = 0; i < str2.length(); i++){ char ch = str2[i]; // If the set of indices of str2[i] is empty, then return -1 if (chars_mp[ch].size() == 0){ return -1; } // If the set of indices of str2[i] is not empty, then find the upper bound of last in the set of indices of str2[i] // It finds the smallest index of str2[i] which is greater than last auto it = chars_mp[ch].upper_bound(last); // If the upper bound is equal to the end of the set, then increment the count and update last to -1 if (it == chars_mp[ch].end()){ last = -1; cnt++; // Decrement I by 1 to process the current character again --i; continue; } // Update last to the current index last = *it; } return cnt; } int main(){ string str1 = "adcb", str2 = "abdca"; int operations = minOperations(str1, str2); cout << "Minimum number of operations required to create string B from the subsequences of the string A is: " << operations << "\n"; return 0; }
输出
Minimum number of operations required to create string B from the subsequences of the string A is: 3
时间复杂度 – O(N*logN),因为我们遍历 str2 并找到循环中“最后一个”索引的上限。
空间复杂度 – O(N),因为我们使用映射来存储字符索引。
以上是将字符串A所需附加的最小子序列以获得字符串B的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

玩家在战双帕弥什中进行游戏时可以获得露西亚深红之渊,有很多玩家不知道露西亚深红之渊怎么获得,玩家可以通过研发获取,或者在幻痛囚笼商店兑换。战双帕弥什露西亚深红之渊怎么获得研发获取1、玩家可以在研发系统中抽取获得,这包括基准卡池、主题限定卡池和命运限定卡池,2、在这些卡池中露西亚·深红之渊的基础掉率为1.50%,但如果玩家在卡池中抽取到露西亚·深红之渊其掉率会增加到1.90%。幻痛囚笼商店兑换1、玩家可以通过在幻痛囚笼商店使用幻痛伤痕来兑换露西亚·深红之渊的碎片。2、每周可以最多兑换30个碎片,集

在Win11系统中获得管理员权限是非常重要的,因为管理员权限可以让用户在系统中执行各种操作,如安装软件、修改系统设置等。在Win11系统中获得管理员权限可以通过以下几种方法实现:第一种方法是通过用户账户控制设置。在Win11系统中,用户账户控制是一个用来管理用户权限的功能,通过它,用户可以调整自己的权限等级。要获得管理员权限,用户可以进入“设置”界面,选择“

标题:Golang中判断字符串是否以指定字符结尾的方法在Go语言中,有时候我们需要判断一个字符串是否以特定的字符结尾,这在处理字符串时十分常见。本文将介绍如何使用Go语言来实现这一功能,同时提供代码示例供大家参考。首先,让我们来看一下Golang中如何判断一个字符串是否以指定字符结尾的方法。Golang中的字符串可以通过索引来获取其中的字符,而字符串的长度可

PHP中int类型转字符串的方法详解在PHP开发中,经常会遇到将int类型转换为字符串类型的需求。这种转换可以通过多种方式实现,本文将详细介绍几种常用的方法,并附带具体的代码示例来帮助读者更好地理解。一、使用PHP内置函数strval()PHP提供了一个内置函数strval(),可以将不同类型的变量转换为字符串类型。当我们需要将int类型转换为字符串类型时,

Go语言是一种强大且灵活的编程语言,它提供了丰富的字符串处理功能,包括字符串截取。在Go语言中,我们可以使用切片(slice)来截取字符串。接下来,将详细介绍如何在Go语言中截取字符串,并附上具体的代码示例。一、使用切片截取字符串在Go语言中,可以使用切片表达式来截取字符串的一部分。切片表达式的语法如下:slice:=str[start:end]其中,s

1、首先打开pycharm,进入到pycharm主页。2、然后新建python脚本,右键--点击new--点击pythonfile。3、输入一段字符串,代码:s="-"。4、接着需要把字符串里面的符号重复20次,代码:s1=s*20。5、输入打印输出代码,代码:print(s1)。6、最后运行脚本,在最底部会看到我们的返回值:-就重复了20次。

解决PHP中16进制转字符串出现中文乱码的方法在PHP编程中,有时候我们会遇到需要将16进制表示的字符串转换为正常的中文字符的情况。然而,在进行这个转换的过程中,有时会遇到中文乱码的问题。这篇文章将为您提供解决PHP中16进制转字符串出现中文乱码的方法,并给出具体的代码示例。使用hex2bin()函数进行16进制转换PHP内置的hex2bin()函数可以将1

托雷特是艾尔登法环这款游戏中的灵马,有很多玩家不知道艾尔登法环托雷特怎么获得,玩家召唤托雷特需要获得灵马哨笛,装备在快捷道具栏后,用快捷键使用即可召唤灵马托雷特。艾尔登法环托雷特怎么获得答:需要获得灵马哨笛。1、玩家召唤托雷特需要获得灵马哨笛。2、玩家从新手出生点来到风暴之路前的赐福点,在篝火旁坐下来,会出现女主角【梅琳娜】,她会给你一个【灵马哨笛】戒指。3、玩家把“灵马哨笛”装备到快捷道具栏后再使用灵马哨笛,就可以召唤托雷特的骏马灵魂了。4、骑上灵马托雷特后,可以进行二段跳,能够跳到走路无法跳
