java数据结构和算法中文第二版
Common.java
package com.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Common {
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
return br.readLine();
}
public static int getInt() throws IOException {
String in = getString();
return Integer.parseInt(in);
}
}
AnagramApp.java
package com.ch6.anagram;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.util.Common;
public class AnagramApp {
private static int size;
private static int count;
private static char[] arrChar = new char[100];
private static List nlist = new ArrayList();
private static List list = new ArrayList();
private static Map<String, Integer> map = new HashMap<String,Integer>() ;
public static void doAnagram(int newSize) {
if (1 == newSize) {
return;
}
for (int i = 0; i < newSize; i++) {
doAnagram(newSize - 1);
System.out.println("第 " + i + "趟" + newSize + " 返回") ;
if (2 == newSize) {
displayWord();
}
rotate(newSize);
}
}
private static void rotate(int newSize) {
int i;
int position = size - newSize;
char temp = arrChar[position];
for (i = position + 1; i < size; i++) {
arrChar[i - 1] = arrChar[i];
}
arrChar[i - 1] = temp;
showWord();
}
private static void showWord() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++) {
sb.append(arrChar[i]);
}
String str = sb.toString();
System.out.println("-->" + str) ;
}
private static void displayWord() {
if (count < 99) {
System.out.print(" ");
}
if (count < 9) {
System.out.print(" ");
}
System.out.print(++count + " ");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++) {
System.out.print(arrChar[i]);
sb.append(arrChar[i]);
}
String str = sb.toString();
Integer n = map.get(str) ;
if (null == n){
map.put(str, 1) ;
}else{
map.put(str, n +1) ;
}
if (nlist.indexOf(str) == -1) {
nlist.add(str);
} else {
if (list.indexOf(str) == -1) {
list.add(str);
}
}
System.out.print(" ");
System.out.flush();
if (count % 6 == 0) {
System.out.println(" ");
}
}
public static void main(String[] args) throws IOException {
while (true) {
System.out.println("Enter a word:");
String input = Common.getString();
size = input.length();
count = 0;
for (int i = 0; i < size; i++) {
arrChar[i] = input.charAt(i);
}
doAnagram(size);
System.out.println("不相同个数:" + nlist.size() + " ,列表如下:");
int count = 0;
for (int i = 0; i < nlist.size(); i++) {
System.out.print(++count + "、 " + nlist.get(i) + " ");
if (count % 6 == 0) {
System.out.println(" ");
}
}
System.out.println(" ");
System.out.println("相同个数:" + list.size() + " ,列表如下:");
count = 0;
for (int i = 0; i < list.size(); i++) {
System.out.print(++count + "、 " + list.get(i) + " ");
if (count % 6 == 0) {
System.out.println(" ");
}
}
System.out.println(" ");
System.out.println(" 次数统计: ");
Set entrySet = map.entrySet() ;
Iterator iter = entrySet.iterator() ;
count = 0 ;
while (iter.hasNext()){
System.out.print(++count + "、 " + iter.next() + " ") ;
if (count % 6 == 0) {
System.out.println(" ");
}
}
System.out.println(" ");
list.clear() ;
nlist.clear() ;
map.clear() ;
}
}
}