查找变位词的Java实现

package test20121006;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Anagram {
	private static final int MAXWORDSCOUNT = 4;
	private int wordscount = 0;
	private WordSignVal[] words = new WordSignVal[Anagram.MAXWORDSCOUNT];
	private Map<String, HashSet<String>> signToValues = new HashMap<String, HashSet<String>>();

	public void receiveWords() {
		BufferedReader stdin = new BufferedReader(new InputStreamReader(
				System.in));
		for (int i = 0; i < Anagram.MAXWORDSCOUNT; i++) {
			System.out.print("请输入第" + (i + 1) + "个单词:");

			try {
				this.words[i] = new WordSignVal();
				this.words[i].setValue(stdin.readLine());
				this.wordscount++;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println(this.words[i].getValue() + " "+
			// this.words[i].getSign());
		}
	}

	public void antoSetWords() {
		this.wordscount = Anagram.MAXWORDSCOUNT;
		this.words[0] = new WordSignVal();
		this.words[0].setValue("ifse");
		this.words[1] = new WordSignVal();
		this.words[1].setValue("acbd");
		this.words[2] = new WordSignVal();
		this.words[2].setValue("dbca");
		this.words[3] = new WordSignVal();
		this.words[3].setValue("seif");
	}

	public void createAnagramSet() {
		int wordsIndex = 0;
		while (wordsIndex < this.wordscount) {
			HashSet<String> values = new HashSet<String>();
			String sign = this.words[wordsIndex].getSign();
			do {
				values.add(this.words[wordsIndex].getValue());
				++wordsIndex;
			} while (wordsIndex < this.wordscount
					&& this.words[wordsIndex].getSign().equals(sign));
			this.signToValues.put(sign, values);
		}
	}

	public void printAnagramSet() {
		Set<String> signSet = this.signToValues.keySet();
		String sign;
		for (Iterator<String> y = signSet.iterator(); y.hasNext();) {
			sign = y.next();
			HashSet<String> values = this.signToValues.get(sign);
			System.out.print(sign + "	:");
			System.out.println(values);
		}
	}

	public void olderByWordSign() {

		for (int i = 0; i < this.wordscount; i++) {
			for (int j = i + 1; j < this.wordscount; j++) {
				if (this.words[i].getSign().compareTo(this.words[j].getSign()) > 0) {
					this.swapTwoWordsPlace(i, j);
				}
			}
		}
		for (int i = 0; i < this.wordscount; i++) {
			System.out.println(this.words[i].getSign() + "	"
					+ this.words[i].getValue());
		}
	}

	public void swapTwoWordsPlace(int m, int n) {
		WordSignVal wv;
		wv = this.words[m];
		this.words[m] = this.words[n];
		this.words[n] = wv;
		// System.out.println(this.words[m].getValue());
		// System.out.println(this.words[n].getValue());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Anagram anagram = new Anagram();
		// anagram.receiveWords();
		// anagram.swapTwoWordsPlace(0, 1);
		anagram.antoSetWords();
		anagram.olderByWordSign();
		anagram.createAnagramSet();
		anagram.printAnagramSet();
	}

	public int getWordscount() {
		return wordscount;
	}
}

class WordSignVal {
	private String sign;
	private String value = "dacb";

	public WordSignVal() {

	}

	public WordSignVal(String value) {
		this.value = value;
		this.valToSign();
	}

	private void valToSign() {
		char[] chararray = value.toCharArray();
		Arrays.sort(chararray);
		this.sign = new String(chararray);
		// System.out.println(sign);
	}

	public String getSign() {
		return sign;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
		this.valToSign();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值