반응형

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
char[] secret;
boolean[] visit;
StringBuilder sb = new StringBuilder();
public void solution() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
secret = new char[c];
visit = new boolean[c];
for(int i=0; i<c; i++){
secret[i] = st.nextToken().charAt(0);
}
Arrays.sort(secret);
dfs("",0,0, c, l);
System.out.println(sb);
}
public void dfs(String token, int x, int depth, int c,int l){
if(depth==l){
int mo = 0,ja = 0;
for(int i=0; i<token.length(); i++){
char text = token.charAt(i);
if(text=='a'||text=='e'||text=='i'||text=='o'||text=='u'){
mo++;
}else{
ja++;
}
}
if(mo>=1&&ja>=2){
sb.append(token).append("\n");
}
return;
}
for(int i=x; i<c; i++){
dfs(token + secret[i], i+1, depth + 1, c, l);
}
}
public static void main(String args[]) throws IOException {
new Main().solution();
}
}
다른 사람들 어떻게 풀었나 봤더니 내 코드 보다 좀 더 간결한거 같다. 방법은 비슷하다.
특정 조건을 만족하면 출력한다. (모음 1개이상 자음 2개이상)
완성된 문자의 조합은 크기 순서대로 나열해야하고, 2번 인덱스의 문자는 1번 인덱스보다 커야 한다.
조건을 만족시키기 위해 index+1로 재귀하며 풀었다.
반응형
'개발 > 알고리즘' 카테고리의 다른 글
백준 15661번 : 링크와 스타트 (0) | 2023.01.16 |
---|---|
백준 14889번 : 스타트와 링크 (0) | 2023.01.16 |
백준 14501번 : 퇴사 (0) | 2023.01.11 |
백준 18290번 : NM과 K (1) (0) | 2023.01.10 |
백준 15656번 : N과 M (7) (0) | 2023.01.06 |