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呀?求指教!谢谢!

Posted by qingyezhu at 2014-10-06 14:52:03 on Problem 2503
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	private static final int LEN = 100010;

	public static void main(String[] args) throws FileNotFoundException {
		
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			List<String> dictList = read(cin);
			List<Node> nodeList = new ArrayList<Node>(LEN);
			for (int i = 0, size = dictList.size(); i < size; i++) {
				String[] str = dictList.get(i).split(" ");
				Node node = new Node(str[0], str[1]);
				nodeList.add(node);
			}
			Collections.sort(nodeList);
			List<String> foreignList = read(cin);
			for (int i = 0, size = foreignList.size(); i < size; i++) {
				int index = binarySearch(nodeList, foreignList.get(i));
				if (index == -1) {
					System.out.println("eh");
				} else {
					System.out.println(nodeList.get(index).getWord());
				}
			}
		}
	}

	private static int binarySearch(List<Node> nodeList, String key) {
		int left = 0, right = nodeList.size() - 1;
		while (left < right) {
			int mid = (left + right) >> 1;
			String foreign = nodeList.get(mid).getForeign();
			int index = foreign.compareTo(key);
			if (index == 0) {
				return mid;
			} else if (index > 0) {
				right = mid - 1;
			} else {
				left = mid + 1;
			}
		}
		return -1;
	}

	private static List<String> read(Scanner cin) {
		List<String> list = new ArrayList<String>(LEN);
		String str = cin.nextLine();
		while (str.length() != 0) {
			list.add(str);
			str = cin.nextLine();
		}
		return list;
	}

}

class Node implements Comparable<Node> {
	private String word;
	private String foreign;

	public String getWord() {
		return word;
	}

	public void setWord(String word) {
		this.word = word;
	}

	public String getForeign() {
		return foreign;
	}

	public Node(String word, String foreign) {
		super();
		this.word = word;
		this.foreign = foreign;
	}

	public void setForeign(String foreign) {
		this.foreign = foreign;
	}

	@Override
	public int compareTo(Node o) {
		int index = this.getForeign().compareTo(o.getForeign());
		if (index > 0) {
			return 1;
		} else if (index < 0) {
			return -1;
		} else {
			return 0;
		}
	}

}

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