代码改变世界

在一个词典中找出所有的变位词

2012-09-13 17:25  jiutianhe  阅读(609)  评论(0编辑  收藏  举报

编程珠玑题:

java实现

package program.pearls;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class DictionaryAnagram {
	/**
	 *	在一个词典中找出所有的变位词,
	 *	例如 pots stop tops互为变位词,
	 *	因为一个单词可以通过改变其他单词中字母的顺序得到其他单词。
	 */
	/**
		我们可以把首先遇到的单词,进行排序。
		例如pans排序后变为anps,我们现在就把anps作为单词pans的标记,
		然后当在遇到snap,也将anps标记为anps。
		也就是将所有只以字母a n p s组成的单词都标记为anps。
		遇到的其他单词也已相同的方法进行标记。
		然后再对所有的标记进行排序,
		就可以肯容易得到一组变位词。
	 */
	public void dictionaryAnagram(String rFilename,String wFilename){
		try {
			FileReader fr=new FileReader(rFilename);
			BufferedReader br=new BufferedReader(fr);
			FileOutputStream fos=new FileOutputStream(wFilename);
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			
			HashSet<String> wordsSet=new HashSet<String>();
			IdentityHashMap<String, String> wordsMap=new IdentityHashMap<String, String>();
			String line=null;
			while((line=br.readLine())!=null){
				String[] sArr=line.trim().split(" ");
				for (String word : sArr) {
					wordsSet.add(word);
				}
			}
			br.close();
			fr.close();
			
			for (String word : wordsSet) {
				char[] sortWordChar=word.toCharArray();
				Arrays.sort(sortWordChar);
				String sortWord=new String(sortWordChar);
				wordsMap.put(sortWord, word);
			}
			
			ArrayList<Map.Entry<String, String>> infoIds =
				    new ArrayList<Map.Entry<String, String>>(wordsMap.entrySet());
			Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {   
				public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
			        return (o1.getKey()).toString().compareTo(o2.getKey());
			    }
			});
			
			ArrayList<String> temp=new ArrayList<String>();			
			for (Entry<String, String> entry : infoIds) {
				if (!temp.contains(entry.getKey())) {
					osw.write("\n");
				}
				temp.add(entry.getKey());
				osw.write(entry.getValue()+"\t");
			}
			osw.write("\n");
			osw.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void dictionaryAnagram2(String rFilename,String wFilename){
		try {
			FileReader fr=new FileReader(rFilename);
			BufferedReader br=new BufferedReader(fr);
			FileOutputStream fos=new FileOutputStream(wFilename);
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			
			HashMap<String, String> wordsMap=new HashMap<String, String>();
			String line=null;
			while((line=br.readLine())!=null){
				String[] sArr=line.trim().split(" ");
				for (String word : sArr) {
					char[] sortWordChar=word.toCharArray();
					Arrays.sort(sortWordChar);
					String sortWord=new String(sortWordChar);
					wordsMap.put(word, sortWord);
				}
			}
			br.close();
			fr.close();
			
			ArrayList<Map.Entry<String, String>> infoIds =
				    new ArrayList<Map.Entry<String, String>>(wordsMap.entrySet());
			Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {   
			    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
			        return (o1.getValue()).compareTo(o2.getValue());
			    }
			});

			ArrayList<String> temp=new ArrayList<String>();			
			for (Entry<String, String> entry : infoIds) {
				if (!temp.contains(entry.getValue())) {
					osw.write("\n");
				}
				temp.add(entry.getValue());
				osw.write(entry.getKey()+"\t");
			}
			osw.write("\n");
			osw.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DictionaryAnagram da=new DictionaryAnagram();
		da.dictionaryAnagram("D:\\360云盘\\CProgram\\ProgrammingPearls\\ch2\\dict.txt", "D:\\360云盘\\CProgram\\ProgrammingPearls\\ch2\\dict2.txt");
		da.dictionaryAnagram2("D:\\360云盘\\CProgram\\ProgrammingPearls\\ch2\\dict.txt", "D:\\360云盘\\CProgram\\ProgrammingPearls\\ch2\\dict3.txt");
	}

}