Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:为什么总是WA?(二叉排序树)

Posted by skyencc at 2010-03-27 08:42:41 on Problem 1002
In Reply To:为什么总是WA?(二叉排序树) Posted by:llx2192716 at 2009-11-15 13:39:23
兄弟啊,我跟你一样杯具!!!也是二叉排序树,一天到晚的WA

import java.util.*;

public class Main_Tree {
	private static boolean outputed = false;
	static class PN {
		private int s = 0;
		private int v = 0;
		private PN left = null;
		private PN right = null;
		
		public PN(String fn) {
			s = 1;
			v = parseString2Int(fn);
		}
		public void increase() {
			s++;
		}
		public String toString() {
			int prefix = v / 10000;
			int suffix = v % 10000;
			return (prefix==0?"000":prefix) + "-" + (suffix==0?"0000":suffix) + " " + s;
		}
		public void insert(PN pn) {
			if(pn == null) return;
			if(this.v > pn.v) {
				if(this.left == null) this.left = pn;
				else this.left.insert(pn);
			} else if(this.v == pn.v) {
				this.increase();
			} else {
				if(this.right == null) this.right = pn;
				else this.right.insert(pn);
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		String pnumber = null;
		PN root = null;
		while(n > 0) {
			cin.nextLine();
			pnumber = cin.next();
			PN pn = new PN(pnumber);
			if(root == null) root = pn;
			else root.insert(pn);
			n--;
		}
		inOrderTraverse(root);
		if(!outputed) System.out.println("No duplicates."); 
	}
	
	private static int parseString2Int(String pn) {
		char [] pna = pn.toCharArray();
		int v = 0;
		int l = pna.length;
		char ch;
		for(int i = 0; i < l; i++) {
			ch = pna[i];
			if(ch == '-') continue;
			else {
				v = v * 10 + parseChar2Int(ch);
			}
		}
		return v;
	}
	
	private static int parseChar2Int(char ch) {
		switch(ch) {
		case 'A' : case 'B' : case 'C' : case '2' : return 2;
		case 'D' : case 'E' : case 'F' : case '3' : return 3;
		case 'G' : case 'H' : case 'I' : case '4' : return 4;
		case 'J' : case 'K' : case 'L' : case '5' : return 5;
		case 'M' : case 'N' : case 'O' : case '6' : return 6;
		case 'P' : case 'R' : case 'S' : case '7' : return 7;
		case 'T' : case 'U' : case 'V' : case '8' : return 8;
		case 'W' : case 'X' : case 'Y' : case '9' : return 9;
		case '1' : return 1;
		case '0' : return 0;
		default : return -1;
		}
	}
	
	private static void inOrderTraverse(PN root) {
		if(root == null) return;
		inOrderTraverse(root.left);
		if(root.s > 1) {System.out.println(root);outputed = true;}
		inOrderTraverse(root.right);
	}
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator