반응형
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
import java.io.*;
import java.util.*;
public class Main {
static int n, r;
static char[] ch;
static char[] in;
static boolean[] visited;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
in = br.readLine().replace(" ","").toCharArray();
Arrays.sort(in);
ch = new char[r];
visited = new boolean[n];
bt(0, 0);
System.out.print(sb);
}
static void bt(int depth, int start){
if (depth == r){
int vowel = 0;
int consonant = 0;
for (char c : ch)
switch (c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowel++;
break;
default:
consonant++;
}
if (vowel>=1 && consonant>=2)
sb.append(String.valueOf(ch)+"\n");
return;
}
for (int i=start; i<n; i++){
if (!visited[i]){
visited[i] = true;
ch[depth] = in[i];
bt(depth+1, i+1);
visited[i] = false;
}
}
}
}
반응형
'Algorithm' 카테고리의 다른 글
프로그래머스 코딩테스트 연습 Level1 - 문자열 내림차순으로 배치하기 [ javascript ] (0) | 2021.01.18 |
---|---|
프로그래머스 코딩테스트 연습 Level1 - 두 개 뽑아서 더하기 [ javascript ] (0) | 2021.01.18 |
백준 1182번 부분수열의 합 [ Java ] (0) | 2021.01.15 |
백준 6603 로또 [ Java ] (0) | 2021.01.14 |
백준 15666번 N과 M (12) [ Java ] (0) | 2021.01.14 |