<!-- lang: js -->
package Anagram_three;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.Arrays; import java.util.TreeSet; public class Anagram { //读取一个文件将单词存入数组
public static void main(String[] args){
try{
System.out.println("设置你的字典******单词间用空格隔开");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String dic=null;
dic = br.readLine();
// FileWriter fw = null; // File f = new File("E:\diction.txt"); // if(!f.exists()){ // f.createNewFile(); // } // fw = new FileWriter("E:\diction.txt"); FileWriter fw = new FileWriter("E:\diction.txt"); BufferedWriter f1 = new BufferedWriter(fw); f1.write(dic, 0, dic.length()); f1.close(); // 设置字典:通过键盘把单词保存到diction.txt中 Reader reader = new FileReader("E:\diction.txt");
//Reader reader = new FileReader("E:\E_DIC.txt");
BufferedReader bf=new BufferedReader(reader); // String string=""; // String str=bf.readLine(); // while(str!=null){ // string +=str; // str=bf.readLine(); // }//读取一个文件将单词存入数组string String [] arry=dic.split(" "); String lab[]=new String[100]; for(int i=0;i<arry.length;i++){ String value=arry[i]; char[] chararray = value.toCharArray(); //把单词变成单个字符 Arrays.sort(chararray); String sign = new String(chararray);//转换成字符串,还原单词 //System.out.println(sign); lab[i]=sign;//lab[]存入的是arry[]字符按字母表排序 } //arry[i]对应lab[i]
//System.out.println(arry[2]); //System.out.println(lab[2]); // for(int i=0;i<arry.length;i++){ // System.out.println(arry[i]+" "+lab[i]);//测试输出字典和标签
// } TreeSet<String> tr=new TreeSet<String>(); for(int i=0;i<lab.length;i++){ if(lab[i]!=null){ tr.add(lab[i]); }//add()不会把相同的加进去 } String[] set=new String[tr.size()]; for(int i=0;i<set.length;i++){ set[i]=tr.pollFirst();//set[]为lab的集合,就是去重工作 // System.out.println(set[i]);测试成功 } //在一个标签set[],输出变位词
// public static void main(String[] args){ // String[] arry={"ate","apl","lpa","eta","tea"}; // String[] lab={"aet","alp","alp","aet","aet"}; // String[] set={"aet","alp"}; for(int j=0;j<set.length;j++){ System.out.printf("属于"+set[j]+"的变位置词语:"+" "); for(int i=0;i<arry.length;i++){
if(lab[i].equals(set[j])) System.out.print(arry[i]+" ");
}
System.out.println();
}
// } //****测试成功 // Arrays.sort(lab);
}catch (FileNotFoundException e){
}catch (IOException e){}
} }