English 中文(简体)
zi zi find find
原标题:Using zip to find the difference between two strings in Scala
  • 时间:2011-11-24 12:31:02
  •  标签:
  • string
  • scala
最佳回答

It is slightly more efficient to use (s1,s2).zipped than (s1 zip s2) since you don t need to create the tuples; instead you create functions that take two arguments.

提高效率,但不能轻松使用<>,即界定自己的习俗,专门叙述:

abstract class StrFold[@specialized T](var result: T) {
  def apply(c1: Char, c2: Char): Unit
  def done: Boolean
}
def strfold[@specialized T](s1: String, s2: String)(folder: StrFold[T]): T = {
  var i = 0
  val L = math.min(s1.length, s2.length)
  while (i < L) {
    folder(s1.charAt(i), s2.charAt(i))
    if (folder.done) return folder.result
    i += 1
  }
  folder.result
}

现在,如果你想找到最漫长的共同预案,你会

class LcpFind extends StrFold(0) {
  var done = false
  def apply(c1: Char, c2: Char) { if (c1 == c2) result += 1 else done = true }
}
def lcp(s1: String, s2: String) = strfold(s1,s2)(new LcpFind)

这在计算无双重替罪羊的低森林碳排放物时,几乎相当于仅仅写出必要或可逆的功能代码。

但是,它应当几乎像现在这样迅速地逐手写低层次的扼制。 (唯一惩罚应为<代码>LcpFind) 每一次提出异议,如果你真的希望通过重新制定<条码><>result到零之间重新编号,或您可修改<条码>、<条码>和<条码>,以执行和称作<条码>的<条码/条码>方法,修改拟命名的输入参数<条码>>> 零/条码>,并采用单独的<条码>-ult<>>>>>/代码>方法。

问题回答

是的,由于两个原因,它会效率较低。

  1. 你们正在编制一个与较短的舱位相同的品位清单。 这将比原来的两个阵列大得多。 如果在某一特性上存在差别,则通常寻找最长久的共同预留或检查方式不需要任何<>>> > > 附加记忆。

  2. 在找到LCP时,你不一定需要在整个扼杀中消退。 自zip 这样做确实需要,显然不太有效率地先抓他们。

但是,这并不意味着你必须使用必要的算法:正常的尾歇性功能算法也将同样发挥作用。

它看上去冰,但可能效率低下(与必要代码相比)。

它复制了两种投入,从而有效地利用了O(n>)空间,同时找到最长久的共同预设点可能是O(1)记忆中的时间。 而且,它需要O(n)时间,而O(len(LCP)时间就足够(尽管最坏情况下的O(n)时间),而且它确实有记忆分配;这是一项昂贵的工作。

现在,我正试图发现,两条插图在一杯中是否有所不同。 您是否建议使用<代码>Zp?

取决于扼杀的时间以及业绩是否至关重要。 头部枪弹可能ok到zip

您可使用<条码>view,在进行<条码>Zp时,可进行zy评价。 (因此,它只能耗尽你所能。)





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...