본문으로 바로가기

[백준 1316] 그룹 단어 체커 -JAVA //le_effort//

category 알고리즘 2020. 3. 11. 22:54

알파벳이 총 25개니

boolean 배열을 25칸의 크기만큼 만들어 줍니다.

visited = new boolean [26]

 

알고리즘 순서는 이렇습니다.

1. 방문을 하지 않은 알파벳을 만난다

1-1 범위에 벗어나지 않으면서 같은 알파벳이 아닐 때까지 인덱스를 올려줍니다

ex) a a a b b b  일 경우 1번째 a를 탐색하면 3번째 a까지 탐색을 끝내야 합니다.

 

2. 1-1 작업을 다 끝나고 나온 알파벳이 이미 방문을 한 알파벳이라면 (그룹 단어가 아닌 경우)

그룹 단어가 아닙니다.

 

그나마 유의할 만한 부분은 범위가 인덱스를 초과하지 않기 위한 24번째 줄입니다.

 

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
import java.io.*;
import java.util.*;
public class Main {
    static int n;
    static int cnt=0;
    static boolean bl;
    static boolean visited[];
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        n = Integer.parseInt(br.readLine());
        
        while(n-- >0) {
            bl = true;
            visited = new boolean[26];
            String str = br.readLine();
            
            for(int i=0; i<str.length(); i++) {
                char ch = str.charAt(i);
                if(visited[ch-'a']) {
                    bl = false;
                }
                visited[ch-'a']=true;
                while(i+1<str.length() &&str.charAt(i+1== ch  ) {
                        i++;
                    }
            }
            if(bl) {
                cnt++;
            }
        }
        System.out.println(cnt);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
cs