hqt

Longest Suffix

hqt
Sep 19th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.math.BigInteger;
  8. import java.util.HashSet;
  9. import java.util.StringTokenizer;
  10.  
  11. /**
  12.  * <b> Algorithm on Codeforces. Problem E div 2 </b> </br>
  13.  *
  14.  * @author Huynh Quang Thao
  15.  *
  16.  */
  17. public class Main {
  18.  
  19.     public static void main(String[] args) throws Exception {
  20.         Main main = new Main();
  21.         main.run();
  22.     }
  23.  
  24.     public void run() throws Exception {
  25.         InputReader sc = null;
  26.  
  27.         sc = new InputReader(System.in);
  28.         sc = new InputReader(new FileInputStream(new File("input.txt")));
  29.  
  30.         int ntestcase = sc.nextInt();
  31.         while (ntestcase-- > 0) {
  32.             int n = sc.nextInt();
  33.             HashSet<String> hash = new HashSet<String>();
  34.             String res = "";
  35.             for (int i = 0; i < n; i++) {
  36.                 String word = sc.next();
  37.                 for (int j = 0; j < Math.min(100, word.length()); j++) {
  38.                     String prefix = word.substring(0, j+1);
  39.                     // already has this suffix before. compare with res
  40.                     if (hash.contains(prefix)) {
  41.                         if (prefix.length() > res.length()) res = prefix;
  42.                         else if (prefix.length() == res.length() && prefix.compareTo(res) < 0) res = prefix;
  43.                     }
  44.                     else hash.add(prefix);
  45.                 }
  46.             }
  47.             System.out.println(res);
  48.         }
  49.     }
  50.  
  51.     static class InputReader {
  52.         public BufferedReader reader;
  53.  
  54.         public StringTokenizer tokenizer;
  55.  
  56.         public InputReader(InputStream stream) {
  57.             reader = new BufferedReader(new InputStreamReader(stream));
  58.             tokenizer = null;
  59.         }
  60.  
  61.         public String next() {
  62.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  63.                 try {
  64.                     tokenizer = new StringTokenizer(reader.readLine());
  65.                 }
  66.                 catch (IOException e) {
  67.                     throw new RuntimeException(e);
  68.                 }
  69.             }
  70.             return tokenizer.nextToken();
  71.         }
  72.  
  73.         public int nextInt() {
  74.             return Integer.parseInt(next());
  75.         }
  76.  
  77.         public double nextDouble() {
  78.             return Double.parseDouble(next());
  79.         }
  80.  
  81.         public float nextFloat() {
  82.             return Float.parseFloat(next());
  83.         }
  84.  
  85.         public long nextLong() {
  86.             return Long.parseLong(next());
  87.         }
  88.  
  89.         public BigInteger nextBigInteger() {
  90.             return new BigInteger(next());
  91.         }
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment