Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/WORDLENGTH

 

algospot.com :: WORDLENGTH

단어 길이 재기 문제 정보 문제 (주의: 이 문제는 TopCoder SRM 202 Div 1 Easy 의 번역입니다) 단어들의 길이는 어떤 문장이 어렵게 쓰여진 문장인지, 쉬운 문장인지를 가르는 데 좋은 척도가 됩니다. 예

www.algospot.com

 

풀이입니다. 🤔

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    private static final char HYPHEN = '-';
    private static final char BLANK = ' ';
 
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int cases = Integer.parseInt(in.readLine());
        while (cases-- > 0) {
            StringBuilder builder = new StringBuilder();
            int n = Integer.parseInt(in.readLine());
            for (int i = 0; i < n; ++i) {
                String line = calibrateInputString(in.readLine());
                for (int j = 0, len = line.length(); j < len; ++j) {
                    int curSize = builder.length();
                    char c = calibrateCharAt(line, j, i + 1 == n);
                    switch (c) {
                        case BLANK:
                        case HYPHEN:
                            if (curSize != 0) {
                                if (isAlphabet(builder.charAt(curSize - 1))) builder.append(c);
                                else builder.setCharAt(curSize - 1, BLANK);
                            }
                            break;
 
                        default:
                            if (curSize == 0 || builder.charAt(curSize - 1!= HYPHEN) builder.append(c);
                            else builder.setCharAt(curSize - 1, c);
                    }
                }
            }
 
 
            String[] words = builder.toString().trim().split(String.valueOf(BLANK));
            double ret = 0;
            if (words.length != 0) {
                for (String word : words) ret += word.length();
                ret /= words.length;
            }
 
            System.out.printf("%.3f\n", ret);
        }
 
        in.close();
    }
 
    private static String calibrateInputString(String line) {
        // line 의 마지막이 Alphabet 이라면, 단어의 끝으로 봐야한다.
        return (!line.isEmpty() && isAlphabet(line.charAt(line.length() - 1))) ? line + BLANK : line;
    }
 
    private static char calibrateCharAt(String str, int index, boolean isEndOfLine) {
        char c = str.charAt(index);
        if (c != HYPHEN) return c;
        return (index + 1 == str.length() && !isEndOfLine) ? HYPHEN : BLANK;
    }
 
    private static boolean isAlphabet(char c) {
        return c >= 'a' && c <= 'z';
    }
}
cs

 

어려운 문제는 아니지만, 주의해야할 테스트 케이스가 있습니다. 
기본 예제와 더불어 아래 테스트 케이스도 통과하는지 살펴볼 필요가 있습니다.

 

5
hello-$
there-$
world$
4
a-$
-$
-$
b$
3
i am ver-$
y sleepy arent-$
 you$
1
jong-man rules$
4
a$ 
b-$
c-$
d$
// [a, bcd]

(주의 : 위 입력의 $ 기호는 줄바꿈을 보여주기 위해 추가한 것이며, 실제 입력에는 존재하지 않습니다.  )

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[CCR] 문자 인식  (0) 2022.01.09
[SPETIAL] Spatial Concepts Test  (0) 2022.01.09
[MVP] Most Valuable Programmer  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
[DIAMOND] 다이아몬드  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/MVP

 

algospot.com :: MVP

Most Valuable Programmer 문제 정보 문제 The season of professional programming league has just ended and it’s time for the MVP(Most Valuable Programmer) voting! There are n newspapers reporters who can vote, and the number of candidate programmers

www.algospot.com

 

풀이입니다. 🤔

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        int[][] prefers = new int[100][10];
        int[] votes = new int[10], positions = new int[100];
        boolean[] eliminated = new boolean[10];
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int n = scanner.nextInt(), m = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    prefers[i][j] = scanner.nextInt() - 1;
                }
            }
 
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    int prefer = -1;
                    while (positions[j] < m && prefer == -1) {
                        // 이 부분 구현을 위해 Queue 를 사용해도 좋다.
                        // 이 코드에서는 제거된 프로그래머를 제외하기 위해 prefers 의 Position 만을 제어하고 있다.
                        if (eliminated[prefers[j][positions[j]]]) ++positions[j];
                        else prefer = prefers[j][positions[j]];
                    }
 
                    if (prefer != -1++votes[prefer];
                }
 
                int minVote = Integer.MAX_VALUE, minCount = 0, clearedCount = 0;
                for (int j = 0; j < m; ++j) {
                    if (eliminated[j]) ++clearedCount;
                    else {
                        if (minVote == votes[j]) ++minCount;
                        else if (minVote > votes[j]) {
                            minCount = 1;
                            minVote = votes[j];
                        }
                    }
                }
                if (m - clearedCount != minCount) {
                    for (int j = 0; j < m; ++j) if (votes[j] == minVote) eliminated[j] = true;
                }
 
                Arrays.fill(votes, 0);
            }
 
            List<Integer> answers = new ArrayList<>();
            for (int i = 0; i < m; ++i) if (!eliminated[i]) answers.add(i + 1);
            for (int i = 0, size = answers.size(); i < size; ++i) {
                System.out.printf("%d%s", answers.get(i), (i + 1 == size) ? "\n" : " ");
            }
 
            Arrays.fill(eliminated, false);
            Arrays.fill(positions, 0);
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[SPETIAL] Spatial Concepts Test  (0) 2022.01.09
[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
[DIAMOND] 다이아몬드  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/MISPELL

 

algospot.com :: MISPELL

Mispelling 문제 정보 문제 Misspelling is an art form that students seem to excel at. Write a program that removes the nth character from an input string. 입력 The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the numb

www.algospot.com

 

풀이입니다. 🤔

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        for (int i = 1; i <= cases; ++i) {
            int number = scanner.nextInt();
            String character = scanner.next();
 
            System.out.printf("%d ", i);
            for (int j = 0, size = character.length(); j < size; ++j) {
                if (j + 1 != number) System.out.print(character.charAt(j));
            }
            System.out.println();
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MVP] Most Valuable Programmer  (0) 2022.01.07
[DIAMOND] 다이아몬드  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
[CANDLESTICK] Candlestick Charts  (0) 2022.01.06
Posted by N'