백준
백준 1251 단어 나누기(Java)
Park DJ
2023. 2. 20. 00:47
https://www.acmicpc.net/problem/1251
1251번: 단어 나누기
알파벳 소문자로 이루어진 단어를 가지고 아래와 같은 과정을 해 보려고 한다. 먼저 단어에서 임의의 두 부분을 골라서 단어를 쪼갠다. 즉, 주어진 단어를 세 개의 더 작은 단어로 나누는 것이다
www.acmicpc.net
Java 코드
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
String[] tmp = new String[3];
String str = sc.nextLine();
for(int i = 1; i < str.length() - 1; i++) {
for(int j = i + 1; j < str.length(); j++) {
String s = "";
tmp[0] = str.substring(0, i);
tmp[1] = str.substring(i, j);
tmp[2] = str.substring(j, str.length());
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
StringBuilder c = new StringBuilder();
a.append(tmp[0]);
b.append(tmp[1]);
c.append(tmp[2]);
s += a.reverse().toString();
s += b.reverse().toString();
s += c.reverse().toString();
list.add(s);
}
}
Collections.sort(list);
System.out.println(list.get(0));
}
}