String Unknown pattern Matching

≯℡__Kan透↙ 提交于 2019-12-11 06:52:31

问题


I want to determine an unknown pattern in a string such as,

s=112468112468112468112468112468.

So in this string, we can clearly see that 112468 is the repeating pattern. I searched on google quite a bit for finding some algorithms to help me, but I could only see ones which find a given pattern in a string such as Boyer-Moore algorithm etc.

What I do now to find these repeating unknown pattern is that,

for(i=0;i<Length of String;i++)
{
  for(j=i+1;j<Length of String;j++)
  {
    if(s[i]==s[j] && s[i+1]==s[j+1] && s[i+2]==s[j+2] && s[i+3]==s[j+3])
    {
       patternlength=j-i;

           for(k=i;k<j;k++)
           {
            pattern[k]=s[i+k]
           }
     }
   }
}

Although this works for the given string by using a comparison window of 4 literals, it may very well not work for some other string. Does anybody know a better solution to this.

Thanks


回答1:


This is not pattern matching, this is pattern recognition, which is fundamentally different and potentially much harder.

However, the simple kind of pattern exhibited by this string could have been found using (Python code):

def find_repeated_pattern(s):
    for i in xrange(1, len(s) / 2):
        if s == s[:i] * (len(s) / i):
            return s[:i]

This is a naive implementation because of all its string copying, but it can be made to work in O(n²) time and constant space.



来源:https://stackoverflow.com/questions/9369220/string-unknown-pattern-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!