问题
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