在这个问题中,我们需要通过重复添加总计数次数来解码给定的字符串。
我们可以采用三种不同的方法来解决问题,并且可以使用两个堆栈或一个堆栈来解决问题。另外,我们可以在不使用两个堆栈的情况下解决问题。
问题陈述 - 我们给出了一个字符串 str ,其中包含左括号和右括号、字母和数字字符。我们需要递归地解码字符串。
以下是解码字符串的模式或规则。
[chars] - “chars”应该在结果字符串中出现 count 次。
示例
输入
str = "2[d]3[c2[n]]";
输出
ddcnncnncnn
说明
首先,我们解码 2[n],得到“2[d]3[cnn]”。
接下来,我们解码 3[cnn]。所以,我们得到“2[d]cnnncnncnn”。
接下来,我们解码 2[d]。所以,我们得到“ddcnnncnncnn”。
输入
5[i]
输出
iiiii
解释- 当我们解码给定的字符串时,我们得到 5 个“I”。
输入
3[fg]
输出
fgfgfg
解释- 当我们解码输入字符串时,我们得到“fg”3次。
方法 1
我们将使用两个堆栈来解决此方法中的问题。当我们得到一个左括号时,我们将其推入堆栈。此外,当我们获取数字字符时,我们将所有数字字符附加到一个有效的正整数并将它们添加到整数堆栈中。之后,当我们得到右括号时,我们从堆栈中弹出整数和字符。
算法
第 1 步- 定义“instSt”堆栈来存储数字,定义“strSt”来存储字符串字符和左括号。此外,初始化“answer”以存储结果字符串,初始化“tempStr”以存储临时字符串。
第 2 步- 开始遍历字符串。
第 3 步- 如果当前字符是数字,则用 0 初始化“num”以存储数字。
步骤 3.1 − 如果第 pth 索引处的字符是数字,则遍历字符串,直到得到字母字符或括号。在循环中,将“num”的先前值乘以 10,并将当前数字添加到其中。
步骤 3.2− 将“p”的值增加 1。
步骤 3.3 - 将数字值推送到“instSt”堆栈。
步骤 4 - 如果第 p 个索引处的字符是右括号,请按照以下步骤操作。
步骤 4.1- 用空字符串初始化“temp_str”。之后,如果‘instSt’不为空,则从堆栈中弹出顶部整数。
步骤 4.2 - 现在,使用循环,直到我们得到左括号或堆栈从“strSt”堆栈变空。另外,将字符附加到“temp_str”。
步骤 4.3 - 如果我们由于“[”而停止对角色进行大便,请将其删除。
步骤 4.4 - 接下来,我们需要在“answer”字符串中附加“temp_Str”“num”次。
步骤 4.5 - 将“answer”字符串的每个字符插入“strSt”堆栈中,并使用空字符串重新初始化它。
步骤 5 − 如果当前字符是左括号,请按照以下步骤操作。
步骤 5.1 − 如果前一个字符是数字,则将“[”推入堆栈“StrSt”。否则,将“[”压入 StrSt 堆栈,并将 1 压入“instSt”堆栈。
第 6 步− 如果我们得到一个字母字符,则将其推入“strSt”堆栈。
第 7 步- 最后,使用循环从“strSt”堆栈中删除所有字符,附加到“answer”字符串,然后返回它。
示例
#includeusing namespace std; string decodeTheString(string alpha) { stack instSt; stack StrSt; string tempStr = "", answer = ""; // Iterate the string for (int p = 0; p < alpha.length(); p++) { int num = 0; // If we find the number, extract the number and push it to the stack if (alpha[p] >= '0' && alpha[p] <= '9') { // Making iterations until we get an alphabetic character while (alpha[p] >= '0' && alpha[p] <= '9') { num = num * 10 + alpha[p] - '0'; p++; } p--; instSt.push(num); } // If the character at the pth index is closing bracket else if (alpha[p] == ']') { tempStr = ""; num = 0; // Pop the number from the stack if (!instSt.empty()) { num = instSt.top(); instSt.pop(); } // Pop the character until we get the opening bracket while (!StrSt.empty() && StrSt.top() != '[') { tempStr = StrSt.top() + tempStr; StrSt.pop(); } // remove the opening bracket if (!StrSt.empty() && StrSt.top() == '[') StrSt.pop(); // Append string to answer for num times for (int j = 0; j < num; j++) answer = answer + tempStr; // Insert the updated string again into the stack for (int j = 0; j < answer.length(); j++) StrSt.push(answer[j]); answer = ""; } // If the character at the pth index is an opening bracket else if (alpha[p] == '[') { if (alpha[p - 1] >= '0' && alpha[p - 1] <= '9') { StrSt.push(alpha[p]); } else { StrSt.push(alpha[p]); instSt.push(1); } } else { // Push alphabetic character in the string stack. StrSt.push(alpha[p]); } } // Pop all the elements, make a string, and return. while (!StrSt.empty()) { answer = StrSt.top() + answer; StrSt.pop(); } return answer; } // starting code int main() { string str = "2[d]3[c2[n]]"; cout << "The resultant string after decoding it is - " << decodeTheString(str) << endl; return 0; }
输出
The resultant string after decoding it is - ddcnncnncnn
时间复杂度− O(n^2),因为我们遍历字符串并不断向堆栈推送和弹出元素。
空间复杂度− 在堆栈中存储元素的 O(n)。
方法2
我们将在这种方法中不使用堆栈来解决问题。另外,我们将使用reverse()方法来反转字符串。
算法
第 1 步- 开始迭代字符串。
第 2 步− 如果第 i 个字符是“]”,则将其推入“answer”字符串。否则,请按照以下步骤操作。
第 3 步- 使用空字符串初始化“temp_Str”。
第 4 步- 继续遍历“answer”字符串,直到该字符串为空或找到“[”字符。另外,继续从“answer”字符串中弹出最后一个字符并将其附加到“temp_Str”字符串中。
第 5 步- 当我们从找到“]”括号的位置向后遍历时,反转“temp_Str”字符串。
第 6 步- 从“answer”字符串中删除最后一个字符以删除“[”字符。
第 7 步- 如果“答案”字符串顶部包含数字,则使用数字生成一个整数,并将其存储在 number 变量中。
第 8 步- 反转数字字符串。
第 9 步- 使用 stoi() 方法将字符串转换为数字。
步骤 10 - 将 temp_Str 字符串附加到答案字符串“number”次。
第 11 步- 返回“答案”字符串。
示例
#includeusing namespace std; string decodeTheString(string alpha) { string answer = ""; // iterate the string characters for (int i = 0; i < alpha.length(); i++) { // for all other characters except the closing bracket if (alpha[i] != ']') { answer.push_back(alpha[i]); } else { // Extract the substring lying within the pair string temp_str = ""; // Keep on popping characters until '[' is found. while (!answer.empty() && answer.back() != '[') { temp_str.push_back(answer.back()); answer.pop_back(); } // get original string by reversing the string reverse(temp_str.begin(), temp_str.end()); // open bracket removal answer.pop_back(); // get integer value before the '[' character string number = ""; // get the number before opening bracket while (!answer.empty() && answer.back() >= '0' && answer.back() <= '9') { number.push_back(answer.back()); answer.pop_back(); } // reverse number string reverse(number.begin(), number.end()); // convert string to integer int numInt = stoi(number); for (int p = 0; p < numInt; p++) { answer += temp_str; } } } return answer; } int main() { string str = "2[d]3[c2[n]]"; cout << "The resultant string after decoding it is - " << decodeTheString(str) << endl; return 0; }
输出
The resultant string after decoding it is − ddcnncnncnn
时间复杂度− O(N^2),因为我们遍历字符串并在循环内使用reverse()方法。
空间复杂度− O(N) 来存储数字和临时字符串。
以上是递归解码一个以计数后跟子字符串编码的字符串的详细内容。更多信息请关注PHP中文网其他相关文章!

C 学习者和开发者可以从StackOverflow、Reddit的r/cpp社区、Coursera和edX的课程、GitHub上的开源项目、专业咨询服务以及CppCon等会议中获得资源和支持。1.StackOverflow提供技术问题的解答;2.Reddit的r/cpp社区分享最新资讯;3.Coursera和edX提供正式的C 课程;4.GitHub上的开源项目如LLVM和Boost提升技能;5.专业咨询服务如JetBrains和Perforce提供技术支持;6.CppCon等会议有助于职业

C#适合需要高开发效率和跨平台支持的项目,而C 适用于需要高性能和底层控制的应用。1)C#简化开发,提供垃圾回收和丰富类库,适合企业级应用。2)C 允许直接内存操作,适用于游戏开发和高性能计算。

C 持续使用的理由包括其高性能、广泛应用和不断演进的特性。1)高效性能:通过直接操作内存和硬件,C 在系统编程和高性能计算中表现出色。2)广泛应用:在游戏开发、嵌入式系统等领域大放异彩。3)不断演进:自1983年发布以来,C 持续增加新特性,保持其竞争力。

C 和XML的未来发展趋势分别为:1)C 将通过C 20和C 23标准引入模块、概念和协程等新特性,提升编程效率和安全性;2)XML将继续在数据交换和配置文件中占据重要地位,但会面临JSON和YAML的挑战,并朝着更简洁和易解析的方向发展,如XMLSchema1.1和XPath3.1的改进。

现代C 设计模式利用C 11及以后的新特性实现,帮助构建更灵活、高效的软件。1)使用lambda表达式和std::function简化观察者模式。2)通过移动语义和完美转发优化性能。3)智能指针确保类型安全和资源管理。

C 多线程和并发编程的核心概念包括线程的创建与管理、同步与互斥、条件变量、线程池、异步编程、常见错误与调试技巧以及性能优化与最佳实践。1)创建线程使用std::thread类,示例展示了如何创建并等待线程完成。2)同步与互斥使用std::mutex和std::lock_guard保护共享资源,避免数据竞争。3)条件变量通过std::condition_variable实现线程间的通信和同步。4)线程池示例展示了如何使用ThreadPool类并行处理任务,提高效率。5)异步编程使用std::as

C 的内存管理、指针和模板是核心特性。1.内存管理通过new和delete手动分配和释放内存,需注意堆和栈的区别。2.指针允许直接操作内存地址,使用需谨慎,智能指针可简化管理。3.模板实现泛型编程,提高代码重用性和灵活性,需理解类型推导和特化。

C 适合系统编程和硬件交互,因为它提供了接近硬件的控制能力和面向对象编程的强大特性。1)C 通过指针、内存管理和位操作等低级特性,实现高效的系统级操作。2)硬件交互通过设备驱动程序实现,C 可以编写这些驱动程序,处理与硬件设备的通信。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

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

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。