问题:如何从具有次线性表现的文档中找到内容体中字符串的存在,以及要查找的字符串必须按顺序进行,或者其关联的id不是按字母顺序排列。
最好我们在PHP和/或JAVA中解决这个问题
trie或Knuth-Pratt-Morris或者boyer-moore实现或其他类似算法是否可以帮助在亚线性时间内找到这些匹配项,如果可以,请告诉我如何进行匹配。
更多细节
列表长度可能是数百万行。 每个字符串可以包含字符(a-z0-9)和空格,即“堆栈溢出”,“stackoverflow” 每个String都有一个唯一的标识符(id),它是一个整数。 {“s”:“stackoverflow”,“#”:“920001”} 匹配或找到的字符串应按其唯一标识符的顺序找到。 另外值得注意的是。字符串列表不会经常更改。内容确实如此。
*示例
字符串数组(920001个唯一字符串)和2个文档示例。从内容中检查列表中是否存在字符串。继续查找匹配项,直到找到3个字符串或列表用尽为止。当在内容中找到一个字符串时,新数组中的字符串与[]
匹配正如你所看到的那样,字符串“stackoverflow”在列表的最后部分很长,但在示例2中我们只匹配字符串,其中一个是stackoverflow,这需要相当长的时间来匹配使用一个简单的字符串数组的循环和匹配。
出于此目的,请将下面的列表视为具有920001行,并且12和920000之间的行中的字符串不包含任何匹配项。
**示例列表
"strings":[
{"s":"Disney World", "#":"1"},
{"s":"Universal Studios", "#":"2"},
{"s":"Disneyland", "id":"3"},
{"s":"Slide", "id":"4"},
{"s":"Disneyland", "id":"5"},
{"s":"Plane", "id":"6"},
{"s":"Walt Disney World", "#":"7"},
{"s":"Florida", "#":"8"},
{"s":"Puerto Rico", "#":"9"},
{"s":"Dominican Republic", "id":"10"},
{"s":"Las Vegas", "#":"11"},
{"s":"Mexico", "#":"12"}
....
....
{"s":"United States", "#":"920000"}
{"s":"stackoverflow", "#":"920001"}
]
**内容示例
content = "Bordered on the west by the Gulf of Mexico and on the east by the Atlantic Ocean, Florida has the longest coastline in the contiguous United States and its geography is dominated by water and the threat of frequent hurricanes. Whether you’re a native or just visiting stackoverflow"
content ="tourist attractions and amusement parks. Slide to the seaside hot spots and abundant nightlife, what you need to stay on top of all of the new developments in the Panhandle State today stackoverflow"
这就是我看到的问题。
答案 0 :(得分:1)
构建suffix tree个内容(合并每个内容的所有后缀树),然后在此后缀树中搜索字符串。
如果使用Ukkonen's algorithm,则为线性(= O(n + m),其中n是内容的大小,m是字符串的大小)。
您无法实现次线性表现,因为如果匹配,您需要至少阅读一次。