【问题标题】:Find most common sub-string pattern in a file在文件中查找最常见的子字符串模式
【发布时间】:2014-09-24 03:46:54
【问题描述】:

给你一个类似的字符串:

input_string = """
HIYourName=this is not true
HIYourName=Have a good day
HIYourName=nope
HIYourName=Bye!"""

查找文件中最常见的子字符串。 这里的答案是“HiYourName=”。 请注意,具有挑战性的部分是 HiYourName= 在字符串中本身不是“单词” 即它没有被它周围的间隔分隔。

所以,澄清一下,这不是最常见的单词问题。

【问题讨论】:

  • 啊哈!是的,子字符串应该超过最小长度,比如至少 6 个字符。
  • 那么,现在我们手头有一个编程问题。尽管请注意,无论您给它什么长度的字符串,返回的子字符串都将是该长度,或者返回的子字符串之一将是。你应该为此做好计划。

标签: python string algorithm data-structures substring


【解决方案1】:

问题与http://acm.hdu.edu.cn/showproblem.php?pid=2459完全相同 解决方案是使用后缀数组或后缀树并使用rmq。

【讨论】:

    【解决方案2】:

    另一个没有导入的蛮力:

    s = """ HIYourName=this is not true HIYourName=Have a good day HIYourName=nope HIYourName=Bye!"""
    
    def conseq_sequences(li):
        seq = []
        maxi = max(s.split(),key=len) # max possible string cannot span across spaces in the string
        for i in range(2, len(maxi)+ 1): # get all substrings from 2 to max possible length
            seq += ["".join(x) for x in (zip(*(li[i:] for i in range(i)))) if " " not in x]
        return max([x  for x in seq if seq.count(x) > 1],key=len) # get longest len string that appears more than once
    print conseq_sequences(s)
    HIYourName=
    

    【讨论】:

      【解决方案3】:

      这是一个简单的蛮力解决方案:

      from collections import Counter
      
      s = " HIYourName=this is not true HIYourName=Have a good day HIYourName=nope HIYourName=Bye!"
      for n in range(1, len(s)):
          substr_counter = Counter(s[i: i+n] for i in range(len(s) - n))
          phrase, count = substr_counter.most_common(1)[0]
          if count == 1:      # early out for trivial cases
              break
          print 'Size: %3d:  Occurrences: %3d  Phrase: %r' % (n, count, phrase)
      

      您的示例字符串的输出是:

      Size:   1:  Occurrences:  10  Phrase: ' '
      Size:   2:  Occurrences:   4  Phrase: 'Na'
      Size:   3:  Occurrences:   4  Phrase: 'Nam'
      Size:   4:  Occurrences:   4  Phrase: 'ourN'
      Size:   5:  Occurrences:   4  Phrase: 'HIYou'
      Size:   6:  Occurrences:   4  Phrase: 'IYourN'
      Size:   7:  Occurrences:   4  Phrase: 'urName='
      Size:   8:  Occurrences:   4  Phrase: ' HIYourN'
      Size:   9:  Occurrences:   4  Phrase: 'HIYourNam'
      Size:  10:  Occurrences:   4  Phrase: ' HIYourNam'
      Size:  11:  Occurrences:   4  Phrase: ' HIYourName'
      Size:  12:  Occurrences:   4  Phrase: ' HIYourName='
      Size:  13:  Occurrences:   2  Phrase: 'e HIYourName='
      

      【讨论】:

        【解决方案4】:

        您可以在线性时间内从字符串中构建后缀树或后缀数组(请参阅http://en.wikipedia.org/wiki/Suffix_tree 和其中的链接),然后在构建后缀树之后,您还可以在线性时间计算中通过深度优先搜索线性时间内所有最长出现的子串的后缀前缀数(子串的出现次数),并将此信息存储在后缀树中的每个节点处。然后,您只需要搜索树找到子字符串的最大出现次数(线性时间),然后返回出现次数最多的最长子字符串(也是线性时间)。

        【讨论】:

          猜你喜欢
          • 2020-02-23
          • 2019-07-16
          • 2023-04-09
          • 1970-01-01
          • 2014-05-24
          • 2017-03-30
          • 1970-01-01
          • 1970-01-01
          • 2012-12-08
          相关资源
          最近更新 更多