找到Reg.表达式。超过 {0,1,2},因此字符串的最后一个符号是字符串 mod 3 上到目前为止的符号之和。

发布于 2025-01-02 03:46:56 字数 582 浏览 0 评论 0原文

我正在自学正式语言(Aho's,Hopcroft),但我在正则表达式方面遇到了困难。

我已经能够处理简单的任务,但这一项提出了挑战,至少对我来说。如果到目前为止你还不会数,如何解决这个问题,我不习惯这种类型的计算。
一定有一些属性或东西可以让我概括答案,以至于我可以将其作为正则表达式。

到目前为止,我已经设计出可能至少有 2 o 3 种情况:

  • sums mod3=0 if sum=3k
  • sums mod3=1 if sum=3k+1
  • sums mod3=2 if sum=3k+2。

但我开始意识到,总和可能有多种组合,因此无法找到正则表达式必须遵循的模式。

ex 的字符串。 {122211}0(大括号是为了便于阅读)末尾有零,因为如果总和为“10”,则表示 {sum=3k}0来自 ex 的字符串。 {1222111}1 情况可能是 {sum=3k+1},因此必须位于末尾,依此类推。

这可能是也可能不是解决问题的正确途径,但我愿意接受任何建议,非常感谢任何帮助。

I'm learning by myself formal languages (Aho's,Hopcroft) but I'm having a hard time with regular expressions.

I've been able to tackle simple tasks but this one has posed a challenge, at least for me. How to solve this if you can't count so far, I'm not used to this type of computation.
There must be some property or something that let me generalize the answer that much that i can put it as a regular expresion.

So far I've devised that is possible that there may be at least 2 o 3 cases:

  • sums mod3=0 if sum=3k
  • sums mod3=1 if sum=3k+1
  • sums mod3=2 if sum=3k+2.

But I've come to realize that there may be many combinations for a sum to happen so can't find the pattern the regular expression must follow.

The string for ex. {122211}0 (braces are for easy read sake) has the zero at the end as it holds that {sum=3k}0, if the sum is "10" from a string for ex. {1222111}1 the case may be {sum=3k+1} so the one has to be at the end, and so on.

This may or not be the right track to tackle the problem but I'm open to any suggestions please, any help is very appreciated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2025-01-09 03:46:56

这里有一个提示:想想你可能处于什么不同的最终状态。你肯定至少有 3 个状态,因为值的数量可以是三个不同的东西模三。此外,您需要有一个不同的开始状态,因为不能接受空字符串。您需要更多州吗?

提示2:我认为您可以使用起始状态和其他九个状态(其中恰好有三个状态将被接受)通过 DFA 轻松完成此操作。

编辑:一旦你有了 DFA,你就可以使用 Kleene 定理来构造一个等效的正则表达式。如果您想直接使用正则表达式,这里有另一个提示:如果您正在查看长度为 3k 的任何字符串,您可以附加: 0;任何长度为 1 且后跟 1 的字符串;任何长度为 2 的字符串,后跟 2。因此,如果您可以为长度为 3k、1 和 2 的字符串编写正则表达式,那么您实际上就完成了。

Here's a hint: think of what distinct final states you can possibly be in. You certainly have at least 3 states, since the number of values can be three different things mod three. Also, you need to have a distinct start state, since the empty string cannot be accepted. Do you need more states?

Hint2: I think you can easily do this with a DFA using a start state and nine other states, of which exactly three will be accepting.

EDIT: Once you have a DFA, you can use Kleene's Theorem to construct an equivalent regular expression. If you'd rather go straight for a regular expression, here's another hint: if you're looking at any string of length 3k, you can append: 0; any string of length 1, followed by 1; any string of length 2, followed by 2. So if you can write regular expressions for strings of lengths 3k, 1, and 2, you're practically done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文