如何找到一个给定数组中重复的字符序列? [英] How to find repeating sequence of characters in a given array?

查看:146
本文介绍了如何找到一个给定数组中重复的字符序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是要找到字符的重复序列给定的数组中为止。简单地说,以识别其中的字符出现的图案。

My problem is to find the repeating sequence of characters in the given array. simply, to identify the pattern in which the characters are appearing.

   .---.---.---.---.---.---.---.---.---.---.---.---.---.---.
1: | J | A | M | E | S | O | N | J | A | M | E | S | O | N |
   '---'---'---'---'---'---'---'---'---'---'---'---'---'---'

   .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
2: | R | O | N | R | O | N | R | O | N | R | O | N | R | O | N |
   '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'

   .---.---.---.---.---.---.---.---.---.---.---.---.
3: | S | H | A | M | I | L | S | H | A | M | I | L |
   '---'---'---'---'---'---'---'---'---'---'---'---'

   .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
4: | C | A | R | P | E | N | T | E | R | C | A | R | P | E | N | T | E | R |
   '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'

鉴于previous数据,结果应该是:

Given the previous data, the result should be:


  1. 将Jameson

  2. RON

  3. SHAMIL



  • 如何有效地处理这个问题?

推荐答案

有关你的例子,我的第一个方法是将

For your examples, my first approach would be to


  1. 获取数组的第一个字符(你的最后一个例子,这将是 C

  2. 获得该字符的下一个出现的指数阵列中(例如9)

  3. 如果它被发现,搜索字符的两仪之间的子串的外表下(在这种情况下,木匠

  4. 如果发现,就大功告成了(并且结果是这样子)。

当然,这仅适用于可能的阵列,其中重复同样的字一遍一遍,从头开始的一个非常有限的子集,而不杂散字符之间,并且它的第一个字符不在字内重复。但是,所有的例子都属于这一类 - 我preFER这有可能工作的最简单的解决方案: - )

Of course, this works only for a very limited subset of possible arrays, where the same word is repeated over and over again, starting from the beginning, without stray characters in between, and its first character is not repeated within the word. But all your examples fall into this category - and I prefer the simplest solution which could possibly work :-)

如果重复字包含第一字符多次(例如仙人掌),该算法可以扩展到寻找该字符的后续出现太,不仅第一一步(因此它找到的全重复的字,不仅是它的一个子)。

If the repeated word contains the first character multiple times (e.g. CACTUS), the algorithm can be extended to look for subsequent occurrences of that character too, not only the first one (so that it finds the whole repeated word, not only a substring of it).

请注意,此扩展算法将给出不同的结果,为您的第二个例子,即 RONRON 而不是 RON

Note that this extended algorithm would give a different result for your second example, namely RONRON instead of RON.

这篇关于如何找到一个给定数组中重复的字符序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆