什么是最有效的算法进行字符串替换?字符串、最有效、算法

2023-09-11 07:19:07 作者:陪你

KMP是搜索,什么是一个用于替换?

KMP is for searching,what's the one for replacing?

推荐答案

更换无非是权正确复制(不匹配)子,而插入取代匹配件(这是一个pretty的简单的任务,完全独立的算法问题 - !)。所以,如果你的知道的KMP是搜索子任务的最好的算法(而不是剪切和干燥的问题,因为你present它,在一般情况下),这也将是最好的替换(特别是如果你取代通过一个新的字符串,你做的万古不易的串像Java和Python语言 - 但尽管如此,即使有一个可变的弦语 - 只要先找到匹配,然后做替换; - 。)

"Replacing" is nothing more than correct copying of the right (non-matched) substrings, while inserting the substitution for the matched pieces (which is a pretty trivial task, quite independent of algorithmic issues!-). So, if you know that KMP is the best algorithm for the searching subtask (not as cut-and-dried an issue as you present it, in the general case), it will also be best for "replacing" (especially if you "replace" by making a new string, as you do in languages with immutable strings like Java and Python -- but, nevertheless, even with a mutable-strings language -- just identify matches first, THEN do the replaces;-).

 
精彩推荐
图片推荐