gpt4 book ai didi

algorithm - 如何同步两个字符串列表中的相等行

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

我有两个希望同步的字符串列表,以便相等的行获得相同的索引,而不同的行将保留在列表中它们原来所在的位置,而另一个字符串列表应该为该索引获取一个“填充符”。考虑这个例子:

SL1:  1,1,2,3,5,8 
SL2: 1,3,5,7,9

procedure SyncStringlists(aSL1,aSL2 : TStringList; aFill : string = '-');

程序应将列表更改为此

SL1:  1,1,2,3,5,8,-,-
SL2: 1,-,-,3,5,-,7,9

或者,如果列表是排序的,那么

SL1:  1,1,2,3,5,-,8,-
SL2: 1,-,-,3,5,7,',9

我应该怎么做?

最佳答案

在列表单调递增的情况下试试这个。

procedure SyncStringlists(SL1, SL2: TStringList; const Fill: string='-');
var
i1, i2: Integer;
begin
i1 := 0;
i2 := 0;
while (i1<SL1.Count) and (i2<SL2.Count) do begin
if SL1[i1]<SL2[i2] then begin
SL2.Insert(i2, Fill);
end else if SL1[i1]>SL2[i2] then begin
SL1.Insert(i1, Fill);
end;
inc(i1);
inc(i2);
end;
while SL1.Count<SL2.Count do begin
SL1.Add(Fill);
end;
while SL2.Count<SL1.Count do begin
SL2.Add(Fill);
end;
end;

关于algorithm - 如何同步两个字符串列表中的相等行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7595400/

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