Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.96 KB | None | 0 0
  1. program start;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. type  pTrie =^Trie;
  9.  
  10.       Trie = record
  11.         letter : char;
  12.         firstchild, nextbrother : pTrie;
  13.       end;
  14.  
  15.       var st : string;
  16.  
  17. procedure CreateTrie(var root : pTrie);
  18. begin
  19.   new(root);
  20.   root^.firstchild := nil;
  21.   root^.nextbrother := nil;
  22. end;
  23.  
  24. procedure PushFront(var head : pTrie; c: char);
  25. var p : pTrie;
  26. begin
  27.   new(p);
  28.   p^.letter := c;
  29.   p^.firstchild := nil;
  30.   p^.nextbrother := head;
  31.   head:=p;
  32. end;
  33.  
  34.  
  35. function find(head : pTrie; c: char) : pTrie;
  36. var pt : pTrie;
  37. begin
  38.   pt := head;
  39.     while ( pt <> nil ) do
  40.       begin
  41.             if(pt^.letter = c ) then
  42.                 break;
  43.           pt:=pt^.nextbrother;
  44.       end;
  45.  result := pt;
  46. end;
  47.  
  48. procedure Insert(root : pTrie; s : string);
  49. var i: integer; n,f: pTrie;
  50. begin
  51.   n := root;
  52.     for i:=1 to length(s) do
  53.       begin
  54.           f := find(n^.firstchild, s[i]);
  55.             if ( f = nil ) then
  56.               begin
  57.                   PushFront(n^.firstchild, s[i]);
  58.                   f := n^.firstchild;
  59.               end;
  60.           n := f;
  61.       end;
  62.     f := find(n^.firstchild, '$');
  63.       if f = nil then
  64.           PushFront(n^.firstchild, '$');
  65. end;
  66.  
  67.  
  68.  
  69.  
  70. procedure PrintTrie(root : pTrie);
  71. var n: pTrie;
  72. begin
  73.     if (root = nil) then exit;
  74.     if (root^.letter = '$') then
  75.       begin
  76.           setlength(st, length(st)-1);
  77.           writeln(st);
  78.       end;
  79.     if ( root^.letter <> '$' ) then
  80.       begin
  81.           st:=st+root^.firstchild^.letter;
  82.           PrintTrie(root^.firstchild);
  83.       end;
  84.     if (root^.nextbrother <> nil) then
  85.       begin
  86.           st := st + root^.nextbrother^.letter;
  87.           PrintTrie(root^.nextbrother);
  88.       end;
  89.  setlength(st, length(st)-1)
  90. end;
  91.  
  92. //--------------------------------------------------------------------
  93.  
  94. procedure readFile(var qq: text; var root: pTrie);
  95. var  flag: boolean;
  96.     word: string;
  97.     c: char;
  98. begin
  99.   Reset(qq);
  100.   flag:=false;
  101.   word:='';
  102.  
  103.     while eof(qq) = false do
  104.       begin
  105.         read(qq, c);
  106.         flag:= true;
  107.           if (c = ',') or (c = ':') or (c = ';') or (c = ' ') or (c = '.')or EOLN(qq) then
  108.               flag:= false;
  109.           if flag = false then
  110.               if word <> '' then
  111.                 begin
  112.                     Insert(root, word);
  113.                     word:='';
  114.                 end;
  115.  
  116.           if flag = true then
  117.             word:= word + c;
  118.       end;
  119.   Close(qq);
  120. end;
  121.  
  122.  
  123. function search(root: pTrie; word: string):boolean;
  124. var i: integer; n,f: pTrie; g: boolean;
  125. begin
  126.   g:= true;
  127.   n := root;
  128.     for i:=1 to length(word) do
  129.       begin
  130.           f := find(n^.firstchild, word[i]);
  131.             if ( f = nil ) then
  132.               begin
  133.                   result:=false;
  134.                   exit;
  135.               end;
  136.           n := f;
  137.       end;
  138.     f := find(n^.firstchild, '$');
  139.       if f = nil then
  140.           g:=false;
  141.     result:=g;
  142. end;
  143.  
  144. procedure testFile(var qq: text; var root: pTrie);
  145. var  flag: boolean;
  146.     word: string;
  147.     c: char;
  148. begin
  149.   Reset(qq);
  150.   flag:=false;
  151.   word:='';
  152.  
  153.     while eof(qq) = false do
  154.       begin
  155.         read(qq, c);
  156.         flag:= true;
  157.           if (c = ',') or (c = ':') or (c = ';') or (c = ' ') or (c = '.')or EOLN(qq) then
  158.               flag:= false;
  159.           if flag = false then
  160.               if word <> '' then
  161.                 begin
  162.                     if (search(root, word)= false) then write('error - ', word);
  163.                     word:='';
  164.                 end;
  165.  
  166.           if flag = true then
  167.             word:= word + c;
  168.       end;
  169.   Close(qq);
  170. end;
  171.  
  172.  
  173. var root : pTrie;
  174.     i : integer;
  175.     //str : array[0..9] of string;
  176.  
  177.     ff: text;
  178.     //t: string;
  179.  
  180. begin
  181.  st:='';
  182.  CreateTrie(root);
  183.  Assign(ff, 'Qwerty.txt');
  184.  
  185.  readFile(ff, root);
  186.  PrintTrie(root);
  187.  readln;
  188.  testfile(ff, root);
  189.  
  190.  
  191.  //readln(t);
  192.  //writeln(search(root, t));
  193.  readln; readln;readln;
  194.  
  195.  
  196.  end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement