일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- zero knowledge proof
- Shamir
- 포토샵
- haze #텐서플로 #tensorflow #ai
- 블로그_이전_계획보다_지금_해야할게_더_많아서_유지예정
- 디자인
- UX
- #암호학이론
- 어도비
- graph 3 coloring
- Adobe
- 완전 비밀 분산
- CC
- 비밀 분산 기법
- 샤미르
Archives
- Today
- Total
For Beginners
[BOJ-1342] 행운의 문자열 본문
728x90
처음에는 중복을 신경써서 풀어야 하는거 아닌가 생각했지만.
중복은 마지막에 제외해주면 되는 것이고, worst의 경우에는 중복이 일어나는 상황이 아니라
모든 문자열이 다 처음 보는 문자인 경우라서, 시간 복잡도에 영향을 주지 않는다는 것을 깨달았다.
package boj.day0323;
import java.util.Arrays;
import java.util.Scanner;
public class BOJ_1342_Main {
static char[] input;
static char[] output;
static boolean[] visited;
static int ans;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
input = new char[s.length()];
int[] alpha = new int[26];
for (int i = 0; i < s.length(); i++) {
input[i] = s.charAt(i);
alpha[s.charAt(i) - 'a']++;
}
visited = new boolean[input.length];
output = new char[s.length()];
ans = 0;
permutation(0);
for (int i = 0; i < alpha.length; i++) {
if (alpha[i] > 1) {
ans = ans / factorial(alpha[i]);
}
}
System.out.println(ans);
sc.close();
}
private static int factorial(int n) {
int r = 1;
for (int i = n; i >= 1; i--) {
r = r * i;
}
return r;
}
private static void permutation(int cnt) {
if (cnt >= input.length) {
// if(output[input.length-1]!=output[input.length-2]) {
for (int j = 1; j < input.length; j++) {
if (output[j] == output[j - 1]) {
return;
}
}
ans++;
System.out.println(Arrays.toString(output));
return;
}
for (int i = 0; i < input.length; i++) {
if (visited[i])
continue;
// if(cnt > 0) {if(input[i] == output[cnt - 1]) continue;}
visited[i] = true;
output[cnt] = input[i];
permutation(cnt + 1);
visited[i] = false;
}
}
}
'2021년 자료 > ALGO' 카테고리의 다른 글
MST와 DIJKSTRA를 구분하는 방법 (0) | 2021.04.16 |
---|---|
[BOJ-14502] 연구소 (0) | 2021.03.26 |
[BOJ-2636] 치즈 (0) | 2021.03.24 |
Comments