开发者

What's the most efficient algorithm for string replacing?

开发者 https://www.devze.com 2022-12-14 04:40 出处:网络
开发者_高级运维KMP is for searching,what\'s the one for replacing?\"Replacing\" is nothing more than correct copying of the right (non-matched) substrings, while inserting the substitution for the mat
开发者_高级运维

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


"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;-).

0

精彩评论

暂无评论...
验证码 换一张
取 消