gpt4 book ai didi

java - 建议将递归 Diff 算法转换为迭代算法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:25:37 24 4
gpt4 key购买 nike

您好!

我从头开始写了一个递归差异算法。它找到两个字符串之间的“最佳匹配”,使差异最小化,并打印出两个字符串,并以 CAPS 表示任何差异。它“按原样”工作正常,只是效率很低。我已经盯着它看了一天半了,试图找到让它迭代的方法,或者至少减少它达到的堆栈深度,但我已经无计可施了,希望这里有一个敏锐的头脑会比我更清楚地看到解决方案。

下面是代码的主要内容。引用的 MergePoint 类只是一个简单的“链表”样式节点,它包含一个“原始索引”整数、一个“更改后的索引”整数和一个“下一个”MergePoint。 MergePoint 列表表示每个数组中已“合并”的一系列索引。当链完成时,链中未表示的任何索引都是插入/删除。 NullObject 对象是 MergePoint 的扩展,回想起来,创建它并不是绝对必要的,基本上可以将其视为常规“null”。

任何意见/建议将不胜感激。

public class StringCompare 
{
public static int[][] mergeList = new int[0][0];
public static MergePoint NULL = NullObject.getNull();
public static int maxMerged = 0;
public static int minClusterSize = -1;

public static void diff(String orig, String alt)
{
String[] original = orig.toUpperCase().split(" ");
String[] altered = alt.toUpperCase().split(" ");

for(int i = 0; i < altered.length; i++)
{
merge(original, altered, 0, i, NULL, NULL, 0, 0);
}


for(int i = 0; i < mergeList.length; i++)
{
or[mergeList[i][0]] = or[mergeList[i][0]].toLowerCase();
al[mergeList[i][1]] = al[mergeList[i][1]].toLowerCase();
}

printStringArray(or);
printStringArray(al);
}

private void printStringArray(String[] arr)
{
for(String word : arr)
{
System.out.print(word.trim() + " ");
}

System.out.println();
}

private static void merge(String[] original, String[] altered, int indexInOriginal, int indexInAltered, MergePoint head, MergePoint tail, int listSize, int clusters)
{
if (indexInOriginal >= original.length)
{
if (listSize > 0)
{
if (((listSize == maxMerged) && (clusters < minClusterSize)) ||
(listSize > maxMerged))
{
storeMergePoints(head, listSize, clusters);
}
}
}
else if (indexInAltered >= altered.length)
{
if (tail != NULL)
{
merge(original, altered, (indexInOriginal + 1), (tail.indexInNew() + 1), head, tail, listSize, clusters);
}
else
{
merge(original, altered, (indexInOriginal + 1), 0, head, tail, listSize, 0);
}
}
else
{
if(original[indexInOriginal].equals(altered[indexInAltered]))
{
MergePoint mergePoint = new MergePoint(indexInOriginal, indexInAltered);
MergePoint bookMark = NULL;
int newClusters = clusters;

if (indexInOriginal != (tail.indexInOriginal() + 1))
{
newClusters++;
}

if (indexInAltered != (tail.indexInNew() + 1))
{
newClusters++;
}

if (head == NULL)
{
head = mergePoint;
tail = head;
}
else
{
tail.setNext(mergePoint);

bookMark = tail;
tail = tail.next();
}

merge(original, altered, (indexInOriginal + 1), (indexInAltered + 1), head, tail, (listSize + 1), newClusters);

if (bookMark == NULL)
{
merge(original, altered, indexInOriginal, (indexInAltered + 1), NULL, NULL, 0, 0);
}
else
{
bookMark.setNext(NULL);

merge(original, altered, indexInOriginal, (indexInAltered + 1), head, bookMark, listSize, newClusters);
}
}
else
{
merge(original, altered, indexInOriginal, (indexInAltered + 1), head, tail, listSize, clusters);
}
}
}

public static void storeMergePoints(MergePoint current, int size, int clusters)
{
mergeList = new int[size][2];
maxMerged = size;
minClusterSize = clusters;

for(int i = 0; i < size; i++)
{
mergeList[i][0] = current.indexInOriginal();
mergeList[i][1] = current.indexInNew();
current = current.next();
}
}
}

最佳答案

为了用迭代替换递归,您可以考虑用您控制的堆栈对象替换 JVM 堆栈:java.util.Stack 非常适合这种情况。只需在每次迭代时 push() 和 pop() 您的数据到该堆栈上,而不是让方法自行调用。

关于java - 建议将递归 Diff 算法转换为迭代算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4765423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com