我有两个要同步的stringlist,这样相等的行得到相同的索引,而不同的行将保留在原来的列表中,而另一个stringlist应该得到该索引的“filler”。举个例子:

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/

10-09 18:41