声明:代码是作者原创,请勿作商业用途
转载请注明出处
A rare book collector recently discovered a book written in an unfamiliar language that used the same characters as the English language. The book contained a short index, but the ordering of the items in the index was different from what one would expect if the characters were ordered the same way as in the English alphabet. The collector tried to use the index to determine the ordering of characters (i.e., the collating sequence) of the strange alphabet, then gave up with frustration at the tedium of the task. 
You are to write a program to complete the collector's work. In particular, your program will take a set of strings that has been sorted according to a particular collating sequence and determine what that sequence is. 
The input consists of an ordered list of strings of uppercase letters, one string per line. Each string contains at most 20 characters. The end of the list is signalled by a line dio is the single character `#'. Not all letters are necessarily used, but the list will imply a complete ordering among those letters that are used. 
Your output should be a single line containing uppercase letters in the order that specifies the collating sequence used to produce the input data file. 
XWY
ZX
ZXY
ZXW
YWWX
#
XZYW
 import java.io.BufferedReader;
import java.io.BufferedReader;
 import java.io.IOException;
import java.io.IOException;
 import java.io.InputStreamReader;
import java.io.InputStreamReader;
 import java.util.ArrayList;
import java.util.ArrayList;


 public class Problem200
public class Problem200  {
{


 /** *//**
    /** *//**
 * @param args
     * @param args
 */
     */

 public static void main(String[] args)
    public static void main(String[] args)  {
{
 ArrayList al = new ArrayList();
        ArrayList al = new ArrayList();        
 
        

 try
        try  {
{

 while(true)
            while(true) {
{
 BufferedReader cin = new BufferedReader( new InputStreamReader( System.in ) );
                BufferedReader cin = new BufferedReader( new InputStreamReader( System.in ) );
 String s = cin.readLine();
                String s = cin.readLine();
 if(s==null || s.length()<1)
                if(s==null || s.length()<1)
 continue;
                    continue;
 
                
 if(s.equals("#"))
                if(s.equals("#"))
 break;
                    break;
 
                

 if(s.length()>20)
                if(s.length()>20) {
{
 System.out.println("length more than 20!");
                    System.out.println("length more than 20!");
 return;
                    return;
 }
                }

 if(!isUpper(s))
                if(!isUpper(s)) {
{
 System.out.println("there are chars not in upper case!");
                    System.out.println("there are chars not in upper case!");
 return;
                    return;
 }
                }
 al.add(s);
                al.add(s);
 }
            }

 } catch (IOException e)
        } catch (IOException e)  {
{
 // TODO Auto-generated catch block
            // TODO Auto-generated catch block
 e.printStackTrace();
            e.printStackTrace();
 }
        }
 
        

 if(al.size()<1)
        if(al.size()<1) {
{
 System.out.println("NONE");
            System.out.println("NONE");
 }
        }
 
        
 char[][] charArr = new char[al.size()][20];
        char[][] charArr = new char[al.size()][20];

 for(int i=0; i<al.size(); i++)
        for(int i=0; i<al.size(); i++) {
{
 charArr[i] = al.get(i).toString().toCharArray();
            charArr[i] = al.get(i).toString().toCharArray();
 }
        }
 
        
 sort(charArr);
        sort(charArr);
 }
    }


 private static void sort(char[][] charArr)
    private static void sort(char[][] charArr)  {
{
 char[] result = new char[26];
        char[] result = new char[26];
 int index = 0;
        int index = 0;
 
        
 char ch;
        char ch;

 for(int i=0; i<charArr.length; i++)
        for(int i=0; i<charArr.length; i++) {
{

 for(int j=0; j<charArr[i].length; j++)
            for(int j=0; j<charArr[i].length; j++) {
{
 ch = charArr[i][j];
                ch = charArr[i][j];

 if(index==0)
                if(index==0) {
{
 result[index] = ch;
                    result[index] = ch;
 index++;
                    index++;

 }else
                }else {
{

 if(contains(ch, result, index))
                    if(contains(ch, result, index)) {
{
 continue;
                        continue;

 }else
                    }else {
{
 result[index] = ch;
                        result[index] = ch;
 index++;
                        index++;
 }
                    }
 }
                }
 }
            }
 }
        }
 StringBuffer buffer = new StringBuffer();
        StringBuffer buffer = new StringBuffer();

 for(int i=0; i<index; i++)
        for(int i=0; i<index; i++) {
{
 buffer.append(result[i]);
            buffer.append(result[i]);
 }
        }
 
        
 System.out.println(buffer.toString());
        System.out.println(buffer.toString());
 }
    }


 private static boolean contains(char ch, char[] result, int index)
    private static boolean contains(char ch, char[] result, int index)  {
{
 boolean flag=false;
        boolean flag=false;

 for(int i=0; i<index; i++)
        for(int i=0; i<index; i++) {
{

 if(result[i]==ch)
            if(result[i]==ch) {
{
 flag=true;
                flag=true;
 break;
                break;
 }
            }
 }
        }
 return flag;
        return flag;
 }
    }


 private static boolean isUpper(String s)
    private static boolean isUpper(String s)  {
{
 boolean result=true;
        boolean result=true;

 for(int i=0; i<s.length(); i++)
        for(int i=0; i<s.length(); i++) {
{
 if(s.charAt(i)>='A' && s.charAt(i)<='Z')
            if(s.charAt(i)>='A' && s.charAt(i)<='Z')
 continue;
                continue;

 else
            else {
{
 result=false;
                result=false;
 break;
                break;
 }
            }
 }
        }
 return result;
        return result;
 }
    }

 }
}

欢迎讨论