unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetPStr(NStr, MStr: string): string;
type
TTree = ^TNode;
TNode = record
c: Char;
LChild, RChild: TTree
end;
var
Root: TTree;
function MakeTree(NStr, MStr: string): TTree;
var
p: Integer;
begin
if NStr='' then
Result:=nil
else
begin
New(Result);
Result^.c:=NStr[Length(NStr)];
p:=Pos(Result^.c, MStr);
Result^.LChild:=MakeTree(Copy(NStr, 1, p-1), Copy(MStr, 1, p-1));
Result^.RChild:=MakeTree(Copy(NStr, p, Length(NStr)-p), Copy(MStr, p+1, Length(NStr)-p))
end
end;
function TravelTree(Root: TTree): string;
begin
if Root=nil then
Result:=''
else
Result:=Root^.c+TravelTree(Root^.LChild)+TravelTree(Root^.RChild)
end;
begin
Root:=MakeTree(NStr, MStr);
Result:=TravelTree(Root)
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit3.Text:=GetPStr(Edit1.Text, Edit2.Text)
end;
end.