递归提取字符串中(嵌套)括号的内容,将所选内容替换为单个(char+int),再次读取并重复

发布于 2025-01-11 06:44:32 字数 1350 浏览 0 评论 0原文

这是我的第一篇文章,所以如果您需要进一步说明,请发表评论,假设我们接受一个字符串,例如:

((((a).(5)).((a)*)).((b )*))*

在这个过程中,我们可能会计算 ++ 读取的 '(' 的数量并计算 - ')' 读取的数量,直到我们遇到第一个字符或变量(我们可以考虑其他运算符,例如“.”或'|' 或 '*') 是左侧最里面的内容,a 被替换,这样我们的字符串现在显示为:

(((R1.(5)) .((a)*)).((b)*))*

我们必须考虑当 a 被选择时,我们也包括它的括号,但只考虑读取的字符串(可能将其存储在或作为向量、指针、对象等),同样适用于 5 ,此时结果为:

(((R1.R2).((a)*)).((b)*))*

我们现在可能发现找到的最里面的内容是字符串 (R1.R2) 并且 这导致字符串被转换为 R3 并具有字符串:

((R3.((a)*)).((b)*))*

我们继续迭代 a 也可以读取。

((R3.(R4*)).((b)*))*

如果读取到星号 *,我们可以考虑使用 R4 提取该内容 被替换为 R5

((R3.R5).((b)*))*

我们的下一次迭代如下:

((R3. R5).(R6*))*

((R3.R5).R7)*

(R8.R7)*

R9*

最后是我们的最终结果,

R10

我们就结束了。

我已经尝试了几个小时的各种算法,但我仍然在同一个地方陷入困境和困惑,而且我可能没有像我希望的那样很好地思考这个问题。

唯一最接近的修改是:

https:// www.geeksforgeeks.org/extract-substrings- Between-any-pair-of-delimiters/

但我仍然对必须正确实施的内容感到困惑。

你将如何使这成为可能?任何解决方案或直接发布您的代码肯定可以帮助我理解这个过程。

This is my first post so please comment down if you need further clarification, Say we take in a string such as:

((((a).(5)).((a)*)).((b)*))*

and through the process, we perhaps count++ the amount of '(' read and count-- the amount of ')' read until we come across our first char or variable (we can consider other operators such as '.' or '|' or '*') that is the left and innermost content such that a is replaced so that our string now reads:

(((R1.(5)).((a)*)).((b)*))*

We must consider when a is selected, we also include its parenthesis as well but only consider the string that is read (perhaps store it in or as a vector, pointer, object, etc.), the same applies for when 5 which results in:

(((R1.R2).((a)*)).((b)*))*

at this moment we perhaps find now that the innermost content found is string (R1.R2) and
this results the string to be converted into R3 and having the string as:

((R3.((a)*)).((b)*))*

We continue the iteration for a to be read too.

((R3.(R4*)).((b)*))*

If a star * is read, we can consider extracting that content with R4 to be replaced as R5

((R3.R5).((b)*))*

Our next iteration follows:

((R3.R5).(R6*))*

((R3.R5).R7)*

(R8.R7)*

R9*

and finally as our final result,

R10

And we end.

I've already tried a variety of algorithms from several sources for hours yet I'm still stuck and puzzled at the same spot and I may not be thinking this through very properly as I had hoped.

The only closest that was modified was this:

https://www.geeksforgeeks.org/extract-substrings-between-any-pair-of-delimiters/

but I'm still puzzled about what must be properly implemented.

How would you make this possible? Any solutions or a straight post of your code could surely help me understand this process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文