From 84df85cc567a01d6fefd58d389c466ad2a9aa1d4 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Sun, 31 Aug 2025 12:21:30 +0900 Subject: [PATCH] =?UTF-8?q?[20250831]=20BOJ=20/=20G5=20/=20=EB=B9=84?= =?UTF-8?q?=EC=8A=B7=ED=95=9C=20=EB=8B=A8=EC=96=B4=20/=20=EC=9D=B4?= =?UTF-8?q?=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\355\225\234 \353\213\250\354\226\264.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "0224LJH/202508/31 BOJ \353\271\204\354\212\267\355\225\234 \353\213\250\354\226\264.md" diff --git "a/0224LJH/202508/31 BOJ \353\271\204\354\212\267\355\225\234 \353\213\250\354\226\264.md" "b/0224LJH/202508/31 BOJ \353\271\204\354\212\267\355\225\234 \353\213\250\354\226\264.md" new file mode 100644 index 00000000..b949e206 --- /dev/null +++ "b/0224LJH/202508/31 BOJ \353\271\204\354\212\267\355\225\234 \353\213\250\354\226\264.md" @@ -0,0 +1,91 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static int size,ansIdx, longest=0; + static String ans1,ans2; + static String[] words; + + static class TrieNode{ + Map children = new HashMap<>(); + String word; + int num; + boolean isWord; + + public TrieNode(String word, int num){ + this.word = word; + this.num = num; + } + } + + static void insert(String nWord, int wordNum){ + TrieNode node = root; + int len = 0; + int tempIdx = 0; + boolean isEnd = false; + String tempWord = ""; + + for (int i = 0; i < nWord.length(); i++){ + Character c = nWord.charAt(i); + if (node.children.containsKey(c)){ + node = node.children.get(c); + if (!isEnd){ + len++; + tempWord = node.word; + tempIdx = node.num; + } + } else { + node.children.put(c, new TrieNode(nWord,wordNum)); + node = node.children.get(c); + isEnd = true; + } + } + + if (longest < len || (longest == len && tempIdx < ansIdx )){ + ansIdx = tempIdx; + longest = len; + ans1 = tempWord; + ans2 = nWord; + } + } + + static TrieNode root = new TrieNode("",-1); + + public static void main(String[] args) throws Exception { + init(); + process(); + print(); + + } + + public static void init() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + size =Integer.parseInt(br.readLine()); + words = new String[size]; + ansIdx = -1; + for (int i = 0; i < size; i++) { + words[i] = br.readLine(); + + } + } + + public static void process(){ + for(int i = 0; i < size; i++){ + insert(words[i],i); +// System.out.println("longest = " + longest); +// System.out.println("ans1 = " + ans1); +// System.out.println("ans2 = " + ans2); + } + } + + public static void print(){ + System.out.println(longest); + System.out.println(ans1); + System.out.println(ans2); + } + + +} +```