contact

 
/*
ID: daniel.20
LANG: JAVA
TASK: contact
 */
import java.io.*;
import java.util.*;

class myComparator implements Comparator<String> {
	public int compare(String o1, String o2) {
		if (o1.length() == o2.length()) {
			return o1.compareTo(o2);
		} else {
			return o1.length() - o2.length();
		}
	}
}

public class contact {
	static int a, b, n;
	static char[] source;
	static int len;

	public static void initial() throws Exception {
		BufferedReader reader = new BufferedReader(new FileReader("contact.in"));
		StringTokenizer st = new StringTokenizer(reader.readLine());
		a = Integer.parseInt(st.nextToken());
		b = Integer.parseInt(st.nextToken());
		n = Integer.parseInt(st.nextToken());
		source = new char[200002];
		String tmp = "";
		int start = 0;
		while ((tmp = reader.readLine()) != null) {
			for (int i = 0; i < tmp.length(); ++i) {
				source[start + i] = tmp.charAt(i);
			}
			start += tmp.length();
		}
		len = start;
	}

	public static void work() throws Exception {
		HashMap<String, Integer> hm = new HashMap<String, Integer>();
		int max = 0;
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"contact.out")));
		for (int i = 0; i < len; ++i) {
			for (int j = a; j <= b; ++j) {
				if (i - j + 1 < 0)
					break;
				String tmp = "";
				for (int k = i - j + 1; k < i + 1; ++k) {
					tmp += source[k];
				}
				if (hm.containsKey(tmp)) {
					int time = hm.get(tmp);
					hm.put(tmp, time + 1);
					max = time + 1 > max ? time + 1 : max;
				} else {
					hm.put(tmp, 1);
				}
			}
		}
		HashMap<Integer, ArrayList<String>> hm1 = new HashMap<Integer, ArrayList<String>>();
		Set<String> keys = hm.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next();
			int value = hm.get(key);
			if (hm1.containsKey(value)) {
				hm1.get(value).add(key);
			} else {
				hm1.put(value, new ArrayList<String>());
				hm1.get(value).add(key);
			}
		}
		Set<Integer> k = hm1.keySet();
		ArrayList<Integer> al = new ArrayList<Integer>(k);
		Collections.sort(al, Collections.reverseOrder());
		int upper = n>al.size()?al.size():n;
		for (int i = 0; i < upper; ++i) {
			ArrayList<String> att = hm1.get(al.get(i));
			out.println(al.get(i));
			Collections.sort(att,new myComparator());
			for (int j = 0; j < att.size(); ++j) {
				if (j != att.size() - 1&&(j+1)%6!=0) {
					out.print(att.get(j) + " ");
				} else {
					out.println(att.get(j));
				}
			}
		}
		out.close();
	}

	public static void main(String[] args) throws Exception {
		initial();
		work();
	}
}


This is not an elegant code. Furture study will be done.

I'll upload another version soon, a bit opertation version, possibly.

阅读更多
个人分类: USACO
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭