关闭

hdu1004字典树

202人阅读 评论(0) 收藏 举报
分类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

import javax.management.Query;


public class Main {

        public static void main(String[] args) {
                InputReader in = new InputReader(System.in) ;
                PrintWriter out = new PrintWriter(System.out);

                int n , m  ;
                while((n = in.nextInt()) != 0){
                       new Task().solve(n, in, out) ;
                }

                out.flush() ;
        }

}


class  Task{
        static class TNode{
              char  c ;
              int   size  ;
              TNode[] next ;
              public TNode(){
                     next = new TNode[26] ;
                     size = 0 ;
              }
              public TNode(char c){
                     this.c = c ; 
                     next = new TNode[26] ;
                     size = 0 ;
              }
        }

        void  solve(int n , InputReader in , PrintWriter out){
             String mx = null ;
             TNode root = new TNode() ;
             int res = 0 ;
             for(int i = 0 ; i < n ; i++){
                  String s = in.next() ;
                  TNode now = root ;
                  for(char c : s.toCharArray()){
                       int d = c - 'a' ;
                       if(now.next[d] == null){
                           now.next[d] = new TNode(c) ;
                       }
                       now = now.next[d] ;     
                  }
                  now.size++ ;
                  if(res <now.size){
                       mx = s ;
                       res = now.size ;
                  }
             }

             out.println(mx) ;
       }
} 

class InputReader{
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream){
           reader = new BufferedReader(new InputStreamReader(stream), 32768);
           tokenizer = null;
    }

    public String next(){
        while(tokenizer == null || !tokenizer.hasMoreTokens()){
            try{
                tokenizer = new StringTokenizer(reader.readLine());
            }catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:211989次
    • 积分:9475
    • 等级:
    • 排名:第1891名
    • 原创:666篇
    • 转载:0篇
    • 译文:0篇
    • 评论:52条
    文章分类
    最新评论