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 alohaking at 2013-11-06 10:54:22 on Problem 1002
In Reply To:谁抽空帮忙看下 Posted by:alohaking at 2013-11-05 16:09:04
import java.util.Scanner;

public class Main {

	private static class Chain{
		String phoneNo;
		int count;
		Chain next;
		public Chain(String phoneNo){
			this.phoneNo = phoneNo;
			this.count = 1;
			this.next = null;
		}
	}
	
	public static void main(String[] args) {
		boolean has = false;
		int n = 0;
		String phoneNo = null;
		char[] temp = null;
		
		Chain head = new Chain("0");
		Chain next = null;
		
		Scanner cin = new Scanner(System.in);
		n = cin.nextInt();
		
		for(int i = 0;i < n; ++i){
			phoneNo = cin.next();
			temp = phoneNo.toCharArray();
			char[] arr = new char[temp.length];
			//替换成对应的数字 好久没写switch居然不会写了
			for(int j = 0,k = 0;j < temp.length;++j){
				if(temp[j] == '-'||temp[j] == 'Q'||temp[j] == 'Z')
					continue;
				if(temp[j] == 'A'||temp[j] == 'B'||temp[j] == 'C'){
					arr[k++]='2';
					continue;
				}else if(temp[j] == 'D'||temp[j] == 'E'||temp[j] == 'F'){
					arr[k++]='3';
					continue;
				}else if(temp[j] == 'G'||temp[j] == 'H'||temp[j] == 'I'){
					arr[k++]='4';
					continue;
				}else if(temp[j] == 'J'||temp[j] == 'K'||temp[j] == 'L'){
					arr[k++]='5';
					continue;
				}else if(temp[j] == 'M'||temp[j] == 'N'||temp[j] == 'O'){
					arr[k++]='6';
					continue;
				}else if(temp[j] == 'P'||temp[j] == 'R'||temp[j] == 'S'){
					arr[k++]='7';
					continue;
				}else if(temp[j] == 'T'||temp[j] == 'U'||temp[j] == 'V'){
					arr[k++]='8';
					continue;
				}else if(temp[j] == 'W'||temp[j] == 'X'||temp[j] == 'Y'){
					arr[k++]='9';
					continue;
				}
				arr[k++] = temp[j];
			}
			phoneNo = String.valueOf(arr).trim();
			
			next = new Chain(phoneNo);
			
			//链表插入排序
			Chain pre = head;
			Chain cur = null;
			
			while(true){
				if(pre.next == null){//如果到了链表的末尾直接将next加入到链表中
					pre.next = next;
					break;
				}else{
					cur = pre.next;//当前元素
					String tPhoneNo = cur.phoneNo;
					if(phoneNo.compareTo(tPhoneNo) == 0){//如果要加入的电话号码与当前元素的电话号码一致
						cur.count++;
						break;
					}else if(phoneNo.compareTo(tPhoneNo) < 0){//如果在链表中找到第一个比phoneNo大的值则插入到当前元素前面
						pre.next = next;
						next.next = cur;
						break;
					}
				}
				pre = cur;
			}
		}
		next  = head.next;
		while(next != null){
			int count = next.count;
			String no = next.phoneNo;
			if(count > 1){
				System.out.println(no.substring(0,3) + "-" + no.substring(3) + " " + count);
				has = true;
			}
			next = next.next;
		}
		if(!has){
			System.out.println("No duplicates.");
		}
	}
}
已经o(n)了怎么还是TLE?

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