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'