Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.math.BigInteger;
- import java.util.HashSet;
- import java.util.StringTokenizer;
- /**
- * <b> Algorithm on Codeforces. Problem E div 2 </b> </br>
- *
- * @author Huynh Quang Thao
- *
- */
- public class Main {
- public static void main(String[] args) throws Exception {
- Main main = new Main();
- main.run();
- }
- public void run() throws Exception {
- InputReader sc = null;
- sc = new InputReader(System.in);
- sc = new InputReader(new FileInputStream(new File("input.txt")));
- int ntestcase = sc.nextInt();
- while (ntestcase-- > 0) {
- int n = sc.nextInt();
- HashSet<String> hash = new HashSet<String>();
- String res = "";
- for (int i = 0; i < n; i++) {
- String word = sc.next();
- for (int j = 0; j < Math.min(100, word.length()); j++) {
- String prefix = word.substring(0, j+1);
- // already has this suffix before. compare with res
- if (hash.contains(prefix)) {
- if (prefix.length() > res.length()) res = prefix;
- else if (prefix.length() == res.length() && prefix.compareTo(res) < 0) res = prefix;
- }
- else hash.add(prefix);
- }
- }
- System.out.println(res);
- }
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream));
- 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());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- public float nextFloat() {
- return Float.parseFloat(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public BigInteger nextBigInteger() {
- return new BigInteger(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment