Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- java
- Math.ceil()
- Math.floor()
- 백준 14391
- java 올림
- 백준 11723
- 자바
- 백준 4375
- Algorithm
- java 내림
- Codility
- mysql
- 네트워크
- 프로그래머스 네트워크 java
- 백준 16927
- 프로그래머스 숫자의 표현 java
- 0으로 채우기
- 프로그래머스 옹알이 java
- sort
- Arrays
- 코딩테스트
- time complexity
- 백준 15661
- 프로그래머스 도둑질 java
- 백준 18290
- 백준 16935
- java 반올림
- 프로그래머스 연속된 수의 합 java
- 백준 17425
- 알고리즘
Archives
- Today
- Total
취미처럼
[백준] 1759번 암호 만들기 본문
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
자음, 모음 수 판별하는 조건 추가
depth가 출력 index 넘지 않도록 조건 추가
import java.util.*;
import java.io.*;
public class Main {
public static int L, C;
public static String[] arr;
public static boolean[] visit;
public static String[] ans;
public static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
L = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
arr = new String[C];
visit = new boolean[C];
ans = new String[L];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < C; i++) {
arr[i] = st.nextToken();
}
Arrays.sort(arr);
dfs(0, 0);
System.out.println(sb);
}
public static void dfs(int start, int depth) {
if(depth == L && isValid()) {
for(int j = 0; j < ans.length; j++) {
sb.append(ans[j]);
}
sb.append("\n");
return;
}
for(int i = start; i < C; i++) {
if(!visit[i] && depth < ans.length) {
visit[i] = true;
ans[depth] = arr[i];
dfs(i + 1, depth + 1);
visit[i] = false;
}
}
}
public static boolean isValid() {
boolean flag = false;
int mo = 0;
int ja = 0;
for(int i = 0 ; i < ans.length; i++) {
if("a".equals(ans[i]) || "e".equals(ans[i]) || "i".equals(ans[i]) || "o".equals(ans[i]) || "u".equals(ans[i]) ) {
mo++;
} else {
ja++;
}
}
if(mo >= 1 && ja >= 2) {
flag = true;
}
return flag;
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 14889번 스타트와 링크 (0) | 2021.03.03 |
---|---|
[백준] 14501번 퇴사 (0) | 2021.03.03 |
[백준] 18290번 NM과 K (1) (0) | 2021.03.03 |
[백준] 15657번 N과 M (8) (0) | 2021.03.03 |
[백준] 15656번 N과 M (7) (0) | 2021.03.03 |
Comments