⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 anagrams2.java

📁 java程序设计
💻 JAVA
字号:
import java.util.*;import java.io.*;public class Anagrams2 {    public static void main(String[] args) {        int minGroupSize = Integer.parseInt(args[1]);         // Read words from file and put into simulated multimap        Map<String, List<String>> m = new HashMap<String, List<String>>();        try {            Scanner s = new Scanner(new File(args[0]));            String word;            while(s.hasNext()) {                String alpha = alphabetize(word = s.next());                List<String> l = m.get(alpha);                if (l==null)                    m.put(alpha, l=new ArrayList<String>());                l.add(word);            }        } catch(IOException e) {            System.err.println(e);            System.exit(1);        }        // Make a List of all permutation groups above size threshold        List<List<String>> winners = new ArrayList<List<String>>();        for (List<String> l : m.values() ) {            if (l.size() >= minGroupSize)                winners.add(l);   }        // Sort permutation groups according to size        Collections.sort(winners, new Comparator<List<String>>() {            public int compare(List<String> o1, List<String> o2) {                return o2.size() - o1.size();            }        });        // Print permutation groups        for (List<String> l : winners ) {            System.out.println(l.size() + ": " + l);        }    }    private static String alphabetize(String s) {        int count[] = new int[256];        int len = s.length();        for (int i=0; i<len; i++)            count[s.charAt(i)]++;        StringBuffer result = new StringBuffer(len);        for (char c='a'; c<='z'; c++)            for (int i=0; i<count[c]; i++)                result.append(c);        return result.toString();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -