Java 250个字符,平均字长约5个字符,总计为:

Java 250个字符,平均字长约5个字符,总计为:,java,Java,(1250*250*5)*(1250*250*5+1)/2=1.2207039*10^12个字符 内存中字符的大小为2个字节,因此大小约为2.22 TB(相比之下,仅小说文本的大小为1.49 MB)。构造MyString时,调用arr=str.tocharray()创建字符串字符数据的新副本。但是在Java中,字符串是不可变的——那么为什么不存储对字符串的引用而不是其数据的副本呢 一次构造每个后缀,但一次只引用一个(嗯,两个)。如果您将解决方案重新编码为仅引用它当前关心的后缀,并且仅在需要它们时

(1250*250*5)*(1250*250*5+1)/2=1.2207039*10^12个字符


内存中字符的大小为2个字节,因此大小约为2.22 TB(相比之下,仅小说文本的大小为1.49 MB)。

构造MyString时,调用
arr=str.tocharray()
创建字符串字符数据的新副本。但是在Java中,字符串是不可变的——那么为什么不存储对字符串的引用而不是其数据的副本呢


一次构造每个后缀,但一次只引用一个(嗯,两个)。如果您将解决方案重新编码为仅引用它当前关心的后缀,并且仅在需要它们时构造它们(并且在以后丢失对它们的引用),那么Java可以对它们进行垃圾收集。这将减少内存不足的可能性。比较存储2个字符串与存储数十万个字符串的内存开销:)

我用Scala编写了这个程序。也许你可以把它翻译成Java

class MyString private (private val string: String, startIndex: Int, endIndex: Int) extends Comparable[MyString] {
  def this(string: String) = this(string, 0, string.length)
  def length() = endIndex-startIndex
  def charAt(i: Int) = {
    if(i >= length) throw new IndexOutOfBoundsException
    string.charAt(startIndex + i)
  }
  def substring(start: Int, end: Int): MyString = {
    if(start < 0 || end > length || end < start) throw new IndexOutOfBoundsException
    new MyString(string, startIndex + start, startIndex + end)
  }
  def substring(start: Int): MyString = substring(start, length)
  def longestCommonSubstring(other: MyString): MyString = {
    var index = 0
    val len = math.min(length, other.length)
    while(index < len && charAt(index) == other.charAt(index)) index += 1
    substring(0, index)
  }
  def compareTo(other: MyString): Int = {
    val len = math.min(length, other.length)
    for(i <- 0 until len) {
      if(charAt(i) > other.charAt(i)) return 1
      if(charAt(i) < other.charAt(i)) return -1
    }
    length-other.length
  }
  def >(other: MyString) = compareTo(other) > 0
  def <(other: MyString) = compareTo(other) < 0
  override def equals(other: Any) = other.isInstanceOf[MyString] && compareTo(other.asInstanceOf[MyString]) == 0
  override def toString() = "\"" + string.substring(startIndex, endIndex) + "\""
}

def readFile(name: String) = new MyString(io.Source.fromFile(name).getLines.mkString(" ").replaceAll("\\s+", " "))
def makeList(str: MyString) = (0 until str.length).map(i => str.substring(i)).toIndexedSeq

val string1 = readFile("WarAndPeace.txt")
val string2 = readFile("MobyDick.txt")

val (list1, list2) = (makeList(string1).sorted, makeList(string2).sorted)

var longestMatch = new MyString("")
var (index1, index2) = (0,0)
while(index1 < list1.size && index2 < list2.size) {
  val lcs = list1(index1).longestCommonSubstring(list2(index2)) 
  if(lcs.length > longestMatch.length) longestMatch = lcs
  if(list1(index1) < list2(index2)) index1 += 1
  else index2 += 1
}

println(longestMatch)
类MyString private(private val string:string,startIndex:Int,endIndex:Int)扩展了Comparable[MyString]{
定义this(string:string)=this(string,0,string.length)
def length()=endIndex startIndex
字符定义(i:Int)={
如果(i>=长度)抛出新的IndexOutOfBoundsException
string.charAt(startIndex+i)
}
def子字符串(开始:Int,结束:Int):MyString={
如果(开始<0 | |结束>长度| |结束<开始)抛出新的IndexOutOfBoundsException
新MyString(字符串,startIndex+start,startIndex+end)
}
def substring(start:Int):MyString=substring(start,length)
def longestCommonSubstring(其他:MyString):MyString={
var指数=0
val len=数学最小值(长度,其他.length)
而(index(其他:MyString)=与(其他)比较>0
def str.substring(i)).toIndexedSeq
val string1=readFile(“WarAndPeace.txt”)
val string2=readFile(“MobyDick.txt”)
val(list1,list2)=(makeList(string1).已排序,makeList(string2).已排序)
var longestMatch=new MyString(“”)
var(index1,index2)=(0,0)
而(index1longestMatch.length)longestMatch=lcs
如果(list1(index1)
for (int i = 0; i < N; i++)
{
    suffixes[i] = snew.substring(i);
}
class MyString private (private val string: String, startIndex: Int, endIndex: Int) extends Comparable[MyString] {
  def this(string: String) = this(string, 0, string.length)
  def length() = endIndex-startIndex
  def charAt(i: Int) = {
    if(i >= length) throw new IndexOutOfBoundsException
    string.charAt(startIndex + i)
  }
  def substring(start: Int, end: Int): MyString = {
    if(start < 0 || end > length || end < start) throw new IndexOutOfBoundsException
    new MyString(string, startIndex + start, startIndex + end)
  }
  def substring(start: Int): MyString = substring(start, length)
  def longestCommonSubstring(other: MyString): MyString = {
    var index = 0
    val len = math.min(length, other.length)
    while(index < len && charAt(index) == other.charAt(index)) index += 1
    substring(0, index)
  }
  def compareTo(other: MyString): Int = {
    val len = math.min(length, other.length)
    for(i <- 0 until len) {
      if(charAt(i) > other.charAt(i)) return 1
      if(charAt(i) < other.charAt(i)) return -1
    }
    length-other.length
  }
  def >(other: MyString) = compareTo(other) > 0
  def <(other: MyString) = compareTo(other) < 0
  override def equals(other: Any) = other.isInstanceOf[MyString] && compareTo(other.asInstanceOf[MyString]) == 0
  override def toString() = "\"" + string.substring(startIndex, endIndex) + "\""
}

def readFile(name: String) = new MyString(io.Source.fromFile(name).getLines.mkString(" ").replaceAll("\\s+", " "))
def makeList(str: MyString) = (0 until str.length).map(i => str.substring(i)).toIndexedSeq

val string1 = readFile("WarAndPeace.txt")
val string2 = readFile("MobyDick.txt")

val (list1, list2) = (makeList(string1).sorted, makeList(string2).sorted)

var longestMatch = new MyString("")
var (index1, index2) = (0,0)
while(index1 < list1.size && index2 < list2.size) {
  val lcs = list1(index1).longestCommonSubstring(list2(index2)) 
  if(lcs.length > longestMatch.length) longestMatch = lcs
  if(list1(index1) < list2(index2)) index1 += 1
  else index2 += 1
}

println(longestMatch)