• 문제 링크
7587번: Anagrams
Output consists of one word from each list, the word with the most anagrams within the list, followed by a space, followed by the number of anagrams. The word displayed will be the first occurrence in the list of the anagram. If more than one word has the
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int t;
while ((t = Integer.parseInt(br.readLine())) != 0) {
Map<String, Integer> map = new HashMap<>();
String[][] mat = new String[t][2];
String s = "";
int max = 0;
for (int i = 0; i < t; i++) {
char[] c = br.readLine().toCharArray();
mat[i][0] = String.valueOf(c);
Arrays.sort(c);
String key = mat[i][1] = String.valueOf(c);
int val = map.getOrDefault(key, -1) + 1;
map.put(key, val);
if (val > 0 && max < val) {
s = key;
max = val;
}
}
for (String[] arr : mat) {
if (arr[1].equals(s)) {
sb.append(arr[0]).append(" ");
break;
}
}
sb.append(max).append("\n");
}
bw.write(sb.toString());
bw.flush();
}
}'Problem Solving > Baekjoon' 카테고리의 다른 글
| [백준] 18422 Emacs - Graph Theory / Java (0) | 2024.02.04 |
|---|---|
| [백준] 4351 Hay Points - Data Structure / Java (0) | 2024.02.03 |
| [백준] 28445 알록달록 앵무새 - Data Structure / Java (0) | 2024.02.01 |
| [백준] 14769 Stacking Cups - Data Structure / Java (0) | 2024.01.31 |
| [백준] 24431 유사 라임 게임 - Data Structure / Java (0) | 2024.01.30 |
댓글