`
128kj
  • 浏览: 583655 次
  • 来自: ...
社区版块
存档分类
最新评论

使用字典树和Hashtable两种方法解POJ 2503(JAVA)

阅读更多
poj2503题意:
   给出一个最多有100000对单词的英语和外语的字典,然后给你一个外语单词 要求你查字典翻译成英语,如果词典里查不到就输出eh。
样例:

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay


Sample Output

cat
eh
loops


解法一:使用jdk中的Hashtable(或HashMap)
 import java.util.Scanner;
import java.util.Hashtable;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        Hashtable< String,String> table = new Hashtable< String, String>();
        String input;
        String [] array=new String[2];
        while(in.hasNext()){
            input=in.nextLine();
            if(input.length()==0)break;
            array=input.split(" ");
            table.put(array[1],array[0]);
        }
        while(in.hasNext()){
           input=in.nextLine();
         if(table.get(input)!=null) System.out.println(table.get(input));
         else System.out.println("eh");
        }
    }
}



方法二:使用字典树
思路:用所有的外语单词去建一棵字典树,然后向这棵字典树中查找给出的单词
import java.util.Scanner;
import java.text.DecimalFormat; 

class Trie{ //字典树
    Trie next[] = new Trie[26];//所有儿子节点
    String  enWord;// 用于记录对应的英语单词  
    public Trie(){
        enWord=null;
    }
} 
 
public class Main{ 
    Trie root = new Trie();
     
     void solve() {
        Scanner  in = new Scanner(System.in);
        String input;
        String [] array=new String[2];
        while(in.hasNext()){//用所有外文字符串,构造字典树
            input = in.nextLine();
            if(input.length()==0) break;
            array=input.split(" ");
            insert(array[1],array[0]);
        }
         while(in.hasNext()){
           input=in.nextLine();
           System.out.println(search(input));
        }
    }

    //建立字典树
    public void insert(String str,String enWord) {  //将一个外文字符串插入字典树
        if (str == null || str.length() == 0) {  
            return ;
        }  
        Trie node = root;  
        char[] letters=str.toCharArray();  
        for (int i = 0; i < str.length(); i++) {  
            int pos = letters[i] - 'a';  
            if (node.next[pos] == null) {  
                node.next[pos] = new Trie();  
                //node.son[pos].val = letters[i];  
            } 
            node = node.next[pos];  
        }  
       //外文字符串的最后一个节点,根节点到此节点构成了一个外文单词,此单词对应的英文单词为enWord;
        node.enWord = enWord;  
    }  

      
     // 在字典树中查找一个完全匹配的外文单词,返回其对应的英文单词.  
    public String search(String str) {  
        if (str == null || str.length() == 0) {  
            return null;  
        }  
        Trie node = root;  
        char[] letters=str.toCharArray();  
        for (int i = 0, len = str.length(); i < len; i++) {  
            int pos = letters[i] - 'a';  
            if (node.next[pos] != null) {  
                node = node.next[pos];  
            } else {  
                return "eh";  
            }  
        }  
        return node.enWord;  
    }  

    
    public static void main(String[] args) {
        Main test = new Main();
         test.solve();
    }
}



0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics