Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
MLE 了,不知有什么改进的方法……const p : array [1..26] of string[1] = ('2', '2','2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'); type pnode = ^tnode; tnode = record left, right : pnode; nums : string[7]; count : longint; end; var root, node : pnode; instr : string; n, i : longint; procedure insert_node(s : string); var i : integer; t : string[7]; x, y : pnode; same : boolean; begin t := ''; while pos('-', s) > 0 do delete(s, pos('-', s), 1); for i := 1 to 7 do begin if (s[i] >= 'A') and (s[i] <= 'Z') then t := t + p[ord(s[i]) - ord('A') + 1]; if (s[i] >= '0') and (s[i] <= '9') then t := t + s[i]; end; new(node); node^.nums := t; node^.count := 1; node^.left := nil; node^.right := nil; y := nil; x := root; same := false; while (x <> nil) do begin y := x; if t < x^.nums then x := x^.left else if t > x^.nums then x := x^.right else if t = x^.nums then begin inc(x^.count); same := true; exit; end; if same then exit; end; if same then exit; if (y = nil) then root := node else if t < y^.nums then y^.left := node else if t > y^.nums then y^.right := node; end; procedure print_tree(x : pnode); var i : byte; begin if x <> nil then begin print_tree(x^.left); if x^.count > 1 then begin for i := 1 to 3 do write(x^.nums[i]); write('-'); for i := 4 to 7 do write(x^.nums[i]); writeln(' ', x^.count); end; print_tree(x^.right); end; end; begin new(root); root := nil; readln(n); for i := 1 to n do begin readln(instr); insert_node(instr); end; print_tree(root); end. Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator