문제
입력 및 출력
풀이
import java.io.*;
import java.util.*;
class Main {
public static int N;
public static int result;
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
result = N;
String[] strArray = new String[N];
for(int i = 0; i < N; i++) {
strArray[i] = br.readLine();
}
Sort(strArray);
for(int i = 0; i < N; i++) {
boolean check = false;
for(int j = i+1; j < N; j++) {
String compWord = strArray[i];
for(int k = 0; k < compWord.length(); k++) {
if(compWord.charAt(k) == strArray[j].charAt(k)) {
check = true;
}else {
check = false;
break;
}
}
if(check) {
break;
}
}
if(check == true) {
result--;
}
}
System.out.println(result);
}
public static void Sort(String[] strArray) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N-1; j++) {
if(strArray[j].length() > strArray[j+1].length()) {
Swap(strArray, j, j+1);
}
}
}
}
public static void Swap(String[] strArray, int i, int j) {
String temp = strArray[i];
strArray[i] = strArray[j];
strArray[j] = temp;
}
}
결과 및 해결방법
[결과]