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

Java解答

Posted by leiliang_2013 at 2010-10-30 07:15:35 on Problem 1002
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) 
	{
		HashMap<String,String> hm = new HashMap<String,String>(35);
		for(int i = 0; i < 10; i ++)
			hm.put(Integer.toString(i), Integer.toString(i));
		String abc = "ABCDEFGHIJKLMNOPRSTUVWXY";
		int value = 1;
		for(int i = 0; i < abc.length(); i ++)
		{
			if(i%3 == 0)
				value += 1;
			hm.put(Character.toString(abc.charAt(i)), Integer.toString(value));
		}
		hm.put("-", "");
		
		Scanner in = new Scanner(System.in);
		int numOfLines = in.nextInt();	
		
		ArrayList<String> arr = new ArrayList<String>();
		String doneLine,line;
		for(int i =0; i < numOfLines; i ++)
		{
			doneLine = "";
			line = in.next();
			for(int j = 0; j < line.length(); j ++)
				doneLine += hm.get(Character.toString(line.charAt(j)));
			arr.add(doneLine);
		}
		
		Collections.sort(arr);	
		boolean dup = false;	
		int num = 1;
		String first = arr.get(0);
		int pointer = 1;
		int arrSize = arr.size();
		while(pointer < arrSize)
		{
			if(first.equals(arr.get(pointer)))
			{
				num += 1;
				pointer += 1;
				if(pointer == arr.size())
				{
					dup = true;
					System.out.println(first.substring(0,3)+"-"+first.substring(3,7)+" "+num);
				}
			}
			else 
			{
				if(num > 1)
				{
					dup = true;
					System.out.println(first.substring(0,3)+"-"+first.substring(3,7)+" "+num);
				}
				num = 1;
				first = arr.get(pointer);
				pointer += 1;
			}
		}
		if(!dup)
			System.out.println("No duplicates.");
	}
}

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