[SPOJ] SBANK – Sorting Bank Accounts

原题: http://www.spoj.com/problems/SBANK/


题目大意:  给n个银行交易帐号信息, 要求排序, 并且统计出现次数, 输出要有序,并且输出次数.


分析: 其实就是设计一个数据结构, 是Key-Value的, 并且有序. Key-Value就是Map了, 有序 自然就是TreeMap.

public void solve(int testNumber, InputReader in, OutputWriter out) {
        int t = in.readInt();
        for (int p = 0; p < t; p++) {
            int n = in.readInt();
            Map<String, Integer> map = new TreeMap<String, Integer>();
            for (int i = 0; i < n; ++i) {
                String s = in.readLine();
                if (!map.containsKey(s))
                    map.put(s,0);
                map.put(s, map.get(s)+1);
            }

            Iterator it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                out.printLine(entry.getKey(), entry.getValue());
            }
            out.printLine();
        }
    }