Algorithm for linear pattern matching?

后端 未结 7 2073
失恋的感觉
失恋的感觉 2021-02-03 15:49

I have a linear list of zeros and ones and I need to match multiple simple patterns and find the first occurrence. For example, I might need to find 0001101101,

7条回答
  •  名媛妹妹
    2021-02-03 16:30

    A solution that could be efficient:

    1. store the patterns in a trie data structure
    2. start searching the list
    3. check if the next pattern_length chars are in the trie, stop on success ( O(1) operation )
    4. step one char and repeat #3

    If the list isn't mutable you can store the offset of matching patterns to avoid repeating calculations the next time.

提交回复
热议问题