728x90
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int L;
static int C;
static String[] arr;
static String[] result;
static String alpa = "aeiou";
static StringBuilder sb = new StringBuilder();
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
L = sc.nextInt();
C = sc.nextInt();
arr = new String[C];
result = new String[L];
for(int i=0; i<C; i++) {
arr[i] = sc.next();
}
Arrays.sort(arr);
DFS(0,0);
System.out.println(sb.toString());
}
static void DFS(int cnt, int start) {
if(cnt==L) {
int mo = 0;
int ja = 0;
for(int i=0; i<L; i++) {
if(alpa.contains(result[i])) {
mo++;
continue;
}
ja++;
if(mo!=0 && ja >=2) {
for(String s : result)
sb.append(s);
sb.append("\n");
return;
}
}
return;
}
for(int i=start; i<C; i++) {
result[cnt] = arr[i];
DFS(cnt+1, i+1);
}
}
}
728x90
'[알고리즘] > 알고리즘' 카테고리의 다른 글
[알고리즘] 백준 1260. DFS와 BFS (0) | 2022.02.23 |
---|---|
[알고리즘] 송아지 찾기 (BFS) (0) | 2022.02.22 |
[알고리즘] 백준 1182. 부분수열의 합 (0) | 2022.02.17 |
[알고리즘] SW Expert Acadamy 5215. 햄버거 다이어트 (0) | 2022.02.17 |
[알고리즘] 백준 1991. 트리순회 (0) | 2022.02.13 |