首页 > 专题系列 > Java解POJ > POJ 1318 Word Amalgamation [解题报告] Java
2013
11-09

POJ 1318 Word Amalgamation [解题报告] Java

Word Amalgamation

问题描述 :

In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.

输入:

The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled ‘words’ that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X’s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.

输出:

For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line “NOT A VALID WORD” instead. In either case, output a line containing six asterisks to signal the end of the list.

样例输入:

tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX

样例输出:

score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******

解题代码:

import java.util.*;

public class Main {

public static void main(String[] args) {
   Scanner cin = new Scanner(System.in);
   List< String> s1 = new ArrayList< String>();
   List< String> s2 = new ArrayList< String>();
   int indexS1 = 0;
   int indexS2 = 0;
   s1.add(cin.nextLine());
   while(s1.get(indexS1).charAt(0) != 'X') {
    indexS1++;
    s1.add(cin.nextLine());
   }
   s1.remove(indexS1);
   Collections.sort(s1);
  
   s2.add(cin.nextLine());
   while(s2.get(indexS2).charAt(0) != 'X') {
    indexS2++;
    s2.add(cin.nextLine());
   }
   s2.remove(indexS2);
  
   for(int i=0; i< s2.size(); i++) {
    boolean out = false;
    for(int j=0; j< s1.size(); j++) {
     if(isSame(s2.get(i), s1.get(j))) {
      System.out.println(s1.get(j));
      out = true;
     }
    }
    if(!out) {
     System.out.println("NOT A VALID WORD");
    }
     System.out.println("******");
   }
  
}

private static boolean isSame(String n1, String n2) {
   if(n1.length() != n2.length())
    return false;
   char[] first = n1.toCharArray();
   char[] secound = n2.toCharArray();
   Arrays.sort(first);
   Arrays.sort(secound);
   for(int i=0; i< first.length; i++) {
    if(first[i] != secound[i])
     return false;
   }
   return true;
}

}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法