728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
String str = sc.next();
LinkedList<Character> list = new LinkedList<>();
for (int i = 0; i < str.length(); i++) {
list.add(str.charAt(i));
}
//System.out.println(list);
int h = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < h; i++) {
int num = sc.nextInt();
map.put(num, map.getOrDefault(num, 0) + 1);
}
//System.out.println(map);
for(int i=str.length();i>=0;i--) {
if(map.containsKey(i)) {
int count=map.get(i);
int num = i;
for(int j=0;j<count;j++) {
list.add(num++,'-');
}
}
}
System.out.print("#" + tc + " ");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));
}
System.out.println();
}
}
}
LinkedList와 HashMap을 사용해서 풀이했다.
list.add를 해줄 때 num++이 아니라 num으로 해주어도 된다.
작대기를 꼭 끝에 붙여줄 필요는 없기 때문이다.
더 간단한 풀이
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
String str = sc.next();
String arr[] = (" " + str).split("");
//System.out.println(Arrays.toString(arr));
int h = sc.nextInt();
for (int i = 0; i < h; i++) {
int num = sc.nextInt();
arr[num] += "-";
}
StringBuilder sb = new StringBuilder();
System.out.print("#"+tc+" ");
for(String n:arr) {
sb.append(n);
}
System.out.println(sb.toString().trim());
}
}
}
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 외로운 문자 (D3) (0) | 2024.09.13 |
|---|---|
| [SW Expert Academy] 현주의 상자 바꾸기 (D3) (0) | 2024.09.13 |
| [SW Expert Academy] [S/W 문제해결 기본] 8일차 - 암호문2 (0) | 2024.09.12 |
| [SW Expert Academy] [S/W 문제해결 기본] 1일차 - View (D3) (0) | 2024.09.12 |
| [SW Expert Academy] 두 수의 덧셈 (D3) (0) | 2024.09.12 |