什么是最有效的算法进行字符串替换? [英] What's the most efficient algorithm for string replacing?

查看:200
本文介绍了什么是最有效的算法进行字符串替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于什么是最有效的算法进行字符串替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆