首页 > 专题系列 > Java解POJ > POJ 1256 Anagram [解题报告] Java
2013
11-09

POJ 1256 Anagram [解题报告] Java

Anagram

问题描述 :

You are to write a program that has to generate all possible words from a given set of letters.

Example: Given the word “abc”, your program should – by exploring all different combination of the three letters – output the words “abc”, “acb”, “bac”, “bca”, “cab” and “cba”.

In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.

输入:

The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.

输出:

For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.

样例输入:

3
aAb
abc
acba

样例输出:

Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa

温馨提示:

An upper case letter goes before the corresponding lower case letter.

So the right order of letters is ‘A’<'a'<'B'<'b'<...<'Z'<'z'.

解题代码:

(1)
import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.Comparator;

 public class Main {

     class Compara implements Comparator {
         public int compare(Character o1, Character o2) {
             char a = Character.toLowerCase(o1);
             char b = Character.toLowerCase(o2);
             if (a == b) {
                 return o1 - o2;
             } else {
                 return a - b;
             }
         }
     }

     public Main() throws NumberFormatException, IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         int t = Integer.parseInt(read.readLine());
         String s;
         Character[] c;
         char[] temp;
         char[] o;
         boolean[] used;
         for (int i = 0; i < t; i++) {
             s = read.readLine();
             o = s.toCharArray();
             c = new Character[o.length];
             for (int j = 0; j < o.length; j++) {
                 c[j] = new Character(o[j]);
             }
             Arrays.sort(c, new Compara());
             used = new boolean[c.length];
             temp = new char[c.length];
             findAll(c, temp, 0, used);
         }
     }

     public void findAll(Character[] c, char[] temp, int i, boolean[] used) {
         char flg = ' ';
         if (i == c.length) {
             System.out.println(temp);
             return;
         }
         for (int j = 0; j < c.length; j++) {
             if (!used[j]) {
                 if (flg == c[j]) {
                     continue;
                 }
                 flg = c[j];
                 temp[i] = c[j];
                 used[j] = true;
                 findAll(c, temp, i + 1, used);
                 used[j] = false;
             }
         }
     }

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         new Main();
     }

 }
(2)

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; public class Main { class Compara implements Comparator { public int compare(Character o1, Character o2) { char a = Character.toLowerCase(o1); char b = Character.toLowerCase(o2); if (a == b) { return o1 - o2; } else { return a - b; } } } public int compareTo(char o1, char o2) { char a = Character.toLowerCase(o1); char b = Character.toLowerCase(o2); if (a == b) { return o1 - o2; } else { return a - b; } } public Main() throws NumberFormatException, IOException { BufferedReader read = new BufferedReader(new InputStreamReader( System.in)); int t = Integer.parseInt(read.readLine()); String s; char[] o; Character[] oc; StringBuilder buff; for (int i = 0; i < t; i++) { s = read.readLine(); o = s.toCharArray(); oc = new Character[o.length]; for (int j = 0; j < o.length; j++) { oc[j] = new Character(o[j]); } Arrays.sort(oc, new Compara()); buff = new StringBuilder(); for (int j = 0; j < o.length; j++) { buff.append(oc[j]); } System.out.println(buff); findAll(buff.toString().toCharArray()); } } public void findAll(char[] c) { int t; int t2; char t3; while (true) { t = -1; for (int i = c.length - 1; i > 0; i--) { if (compareTo(c[i], c[i - 1]) > 0) { t = i; break; } } if (t == -1) { break; } for (int i = c.length - 1;; i--) { if (compareTo(c[i], c[t - 1]) > 0) { t2 = i; break; } } t3 = c[t2]; c[t2] = c[t - 1]; c[t - 1] = t3; for (int j = c.length - 1, i = t; i < j; i++, j--) { t3 = c[j]; c[j] = c[i]; c[i] = t3; } System.out.println(c); } } public static void main(String[] args) throws NumberFormatException, IOException { new Main(); } }

  1. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了