Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program start;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type pTrie =^Trie;
- Trie = record
- letter : char;
- firstchild, nextbrother : pTrie;
- end;
- var st : string;
- procedure CreateTrie(var root : pTrie);
- begin
- new(root);
- root^.firstchild := nil;
- root^.nextbrother := nil;
- end;
- procedure PushFront(var head : pTrie; c: char);
- var p : pTrie;
- begin
- new(p);
- p^.letter := c;
- p^.firstchild := nil;
- p^.nextbrother := head;
- head:=p;
- end;
- function find(head : pTrie; c: char) : pTrie;
- var pt : pTrie;
- begin
- pt := head;
- while ( pt <> nil ) do
- begin
- if(pt^.letter = c ) then
- break;
- pt:=pt^.nextbrother;
- end;
- result := pt;
- end;
- procedure Insert(root : pTrie; s : string);
- var i: integer; n,f: pTrie;
- begin
- n := root;
- for i:=1 to length(s) do
- begin
- f := find(n^.firstchild, s[i]);
- if ( f = nil ) then
- begin
- PushFront(n^.firstchild, s[i]);
- f := n^.firstchild;
- end;
- n := f;
- end;
- f := find(n^.firstchild, '$');
- if f = nil then
- PushFront(n^.firstchild, '$');
- end;
- procedure PrintTrie(root : pTrie);
- var n: pTrie;
- begin
- if (root = nil) then exit;
- if (root^.letter = '$') then
- begin
- setlength(st, length(st)-1);
- writeln(st);
- end;
- if ( root^.letter <> '$' ) then
- begin
- st:=st+root^.firstchild^.letter;
- PrintTrie(root^.firstchild);
- end;
- if (root^.nextbrother <> nil) then
- begin
- st := st + root^.nextbrother^.letter;
- PrintTrie(root^.nextbrother);
- end;
- setlength(st, length(st)-1)
- end;
- //--------------------------------------------------------------------
- procedure readFile(var qq: text; var root: pTrie);
- var flag: boolean;
- word: string;
- c: char;
- begin
- Reset(qq);
- flag:=false;
- word:='';
- while eof(qq) = false do
- begin
- read(qq, c);
- flag:= true;
- if (c = ',') or (c = ':') or (c = ';') or (c = ' ') or (c = '.')or EOLN(qq) then
- flag:= false;
- if flag = false then
- if word <> '' then
- begin
- Insert(root, word);
- word:='';
- end;
- if flag = true then
- word:= word + c;
- end;
- Close(qq);
- end;
- function search(root: pTrie; word: string):boolean;
- var i: integer; n,f: pTrie; g: boolean;
- begin
- g:= true;
- n := root;
- for i:=1 to length(word) do
- begin
- f := find(n^.firstchild, word[i]);
- if ( f = nil ) then
- begin
- result:=false;
- exit;
- end;
- n := f;
- end;
- f := find(n^.firstchild, '$');
- if f = nil then
- g:=false;
- result:=g;
- end;
- procedure testFile(var qq: text; var root: pTrie);
- var flag: boolean;
- word: string;
- c: char;
- begin
- Reset(qq);
- flag:=false;
- word:='';
- while eof(qq) = false do
- begin
- read(qq, c);
- flag:= true;
- if (c = ',') or (c = ':') or (c = ';') or (c = ' ') or (c = '.')or EOLN(qq) then
- flag:= false;
- if flag = false then
- if word <> '' then
- begin
- if (search(root, word)= false) then write('error - ', word);
- word:='';
- end;
- if flag = true then
- word:= word + c;
- end;
- Close(qq);
- end;
- var root : pTrie;
- i : integer;
- //str : array[0..9] of string;
- ff: text;
- //t: string;
- begin
- st:='';
- CreateTrie(root);
- Assign(ff, 'Qwerty.txt');
- readFile(ff, root);
- PrintTrie(root);
- readln;
- testfile(ff, root);
- //readln(t);
- //writeln(search(root, t));
- readln; readln;readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement