Skip to content

Commit bc1afcc

Browse files
authored
[Week14] PGS 64064: 불량사용자
[Week14] PGS 64064: 불량사용자
2 parents 0285878 + bef6203 commit bc1afcc

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package study.week14;
2+
3+
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
7+
// 불량 사용자
8+
// 백트래킹
9+
10+
/**
11+
* 처음에는 visited를 ban에 두고 백트래킹 했는데, 테스트 케이스 하나가 삑남
12+
* 이유는 중복된 id 세트 처리 미흡, isMatch 함수에서 첫 번째로 매칭되는 인덱스만 반환
13+
* 그래서 한 유저가 여러 밴픽에 포함될 수 있음에도 무조건 첫번째만 리턴하는 문제
14+
* ai 활용 개선 이후
15+
* 1. resultSet을 활용하여, 다른 밴픽을 활용했지만 결과가 같은 문제를 막기 위해, user_id 세트를 string으로 저장해 놓음
16+
* 2. ban픽이 아닌 user기반 visited 배열 생성, 밴아이디 기반으로 유저를 매핑하는 방식으로 변경
17+
* 3. 즉, 밴픽을 전부 사용해야하는 것을 기저 조건으로, user를 매핑 -> 유저 조합 중, 동일한 id에 대해서는 중복 없음 처리(set)
18+
*/
19+
public class PGS_64064 {
20+
21+
public static void main(String[] args) {
22+
String[] user_id = {"frodo", "fradi", "crodo", "abc123", "frodoc"};
23+
String[] ban_id = {"fr*d*", "*rodo", "******", "******"};
24+
25+
int result = solution(user_id, ban_id);
26+
System.out.println(result);
27+
}
28+
29+
static HashSet<String> resultSet = new HashSet<>();
30+
static boolean[] userVisited;
31+
32+
static public int solution(String[] user_id, String[] banned_id) {
33+
userVisited = new boolean[user_id.length];
34+
backtrack(0, "", user_id, banned_id);
35+
return resultSet.size();
36+
}
37+
38+
static void backtrack(int banIdx, String currentSet, String[] user_id, String[] banned_id){
39+
if(banIdx == banned_id.length){
40+
String[] sortedIds = currentSet.trim().split(" ");
41+
Arrays.sort(sortedIds);
42+
resultSet.add(Arrays.toString(sortedIds));
43+
return;
44+
}
45+
46+
for(int i=0; i<user_id.length; i++){
47+
if(!userVisited[i] && isMatch(user_id[i], banned_id[banIdx])){
48+
userVisited[i] = true;
49+
backtrack(banIdx + 1, currentSet + " " + i, user_id, banned_id);
50+
userVisited[i] = false;
51+
}
52+
}
53+
}
54+
55+
static boolean isMatch(String user, String ban){
56+
if(user.length() != ban.length()) return false;
57+
for(int i=0; i<user.length(); i++){
58+
if(ban.charAt(i) != '*' && user.charAt(i) != ban.charAt(i)) return false;
59+
}
60+
return true;
61+
}
62+
}

0 commit comments

Comments
 (0)