Poj1002

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.Scanner;


/*4873279
4873279
8884567
3101010
8884567
8884567
9671111
3104466
3101010
8881200
4873279*/

public class Main {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		Map<String,Integer> map = new HashMap<String, Integer>();
		int times = scan.nextInt();
		
		while(times--!=0){
			String str = scan.next();
			String changestr = change(str);
			Integer time = map.get(changestr);
			map.put(changestr, time==null ? 1:time+1);
		}
	/*	Iterator iter = map.entrySet().iterator();
		boolean flag = false;
		while(iter.hasNext()){
			Map.Entry<String, Integer> entry = (Entry<String, Integer>) iter.next();
			int value = entry.getValue();
			if(value>1){
				flag = true;
				System.out.println(entry.getKey()+" "+value);
			}
		}//while
		if(!flag){
			System.out.println("No duplicates.");
		}*/
		Set<String> keys = new TreeSet<String>(map.keySet());
		boolean hasOutput = false;
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String key = it.next();
			if (map.get(key) > 1) {
				hasOutput = true;
				System.out.println(key+ " " + map.get(key));
			}
		}
		if (!hasOutput) {
			System.out.println("No duplicates.");
		}
	}

	private static String change(String str) {
		
		char[] arr = str.toCharArray();
		int count = 0;
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<arr.length;i++){
			if(arr[i]=='0'){
				if(count==2){
					sb.append('0');
					sb.append('-');
					count++;
				}else{
					sb.append('0');
					count++;
				}
			}
			if(arr[i]=='1'){
				if(count==2){
					sb.append('1');
					sb.append('-');
					count++;
				}else{
					sb.append('1');
					count++;
				}
			}
			if(arr[i]=='2'||arr[i]=='A'||arr[i]=='B'||arr[i]=='C'){
				if(count==2){
					sb.append('2');
					sb.append('-');
					count++;
				}else{
					sb.append('2');
					count++;
				}
			}
			if(arr[i]=='3'||arr[i]=='D'||arr[i]=='E'||arr[i]=='F'){
				if(count==2){
					sb.append('3');
					sb.append('-');
					count++;
				}else{
					sb.append('3');
					count++;
				}
			}
			if(arr[i]=='4'||arr[i]=='H'||arr[i]=='I'||arr[i]=='G'){
				if(count==2){
					sb.append('4');
					sb.append('-');
					count++;
				}else{
					sb.append('4');
					count++;
				}
			}
			if(arr[i]=='5'||arr[i]=='J'||arr[i]=='K'||arr[i]=='L'){
				if(count==2){
					sb.append('5');
					sb.append('-');
					count++;
				}else{
					sb.append('5');
					count++;
				}
			}
			if(arr[i]=='6'||arr[i]=='M'||arr[i]=='N'||arr[i]=='O'){
				if(count==2){
					sb.append('6');
					sb.append('-');
					count++;
				}else{
					sb.append('6');
					count++;
				}
			}
			if(arr[i]=='7'||arr[i]=='P'||arr[i]=='R'||arr[i]=='S'){
				if(count==2){
					sb.append('7');
					sb.append('-');
					count++;
				}else{
					sb.append('7');
					count++;
				}
			}
			if(arr[i]=='8'||arr[i]=='T'||arr[i]=='U'||arr[i]=='V'){
				if(count==2){
					sb.append('8');
					sb.append('-');
					count++;
				}else{
					sb.append('8');
					count++;
				}
			}
			if(arr[i]=='9'||arr[i]=='W'||arr[i]=='X'||arr[i]=='Y'){
				if(count==2){
					sb.append('9');
					sb.append('-');
					count++;
				}else{
					sb.append('9');
					count++;
				}
			}
			
		}
		return sb.toString();
	}

}


最后使用TreeSet进行升序排序。

简单介绍下TreeSet:


TreeSet是一个有序集合,TreeSet中的元素为升序排列。要使其变成降序排列,需要实现Comparator接口。

代码:

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


public class TreeSetTest {

	//TreeSet的用法
	public static void main(String[] args) {
		
		String[] arr = new String[]{"zhangsan","lisi","wangwu"};
		TreeSet treeSet1 = new TreeSet();
		
		for(int i=0;i<arr.length;i++){
			treeSet1.add(arr[i]);
		}
		
		//升序
		Iterator<String> it = treeSet1.iterator();
		while(it.hasNext()){
			String str = it.next();
			System.out.println(str);
		}
		
		System.out.println(treeSet1);
		
		TreeSet treeSet2 = new TreeSet(new Comparator() {

			@Override
			public int compare(Object a, Object b) {
				String str1 = (String)a;
				String str2 = (String)b;
				if(str1.compareTo(str2)>0){
					return -1;
				}
				if(str1.compareTo(str2)<0){
					return 1;
				}
				return 0;
			}
			
		});
		for(int i=0;i<arr.length;i++){
			treeSet2.add(arr[i]);
		}	
		//降序
		System.out.println(treeSet2);
	}

}




你可能感兴趣的:(TreeSet,POJ1002)