알고리즘/백준 알고리즘

[java] 백준 알고리즘 1316번 그룹 단어 체크 풀이

희랍인 조르바 2018. 5. 23. 16:59



* 풀이 소스


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
public class Baekjoon1316 {
 
    static StringTokenizer st;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken()); // 단어의 개수
        int groupWordCount = n; // 그룹단어 수
        
        
        for(int i=0; i<n; i++) {
            st = new StringTokenizer(br.readLine());
            String word = st.nextToken();
            
            int[] checkArray = new int[26];
            checkArray[word.charAt(0- 97= -1// 초기값 설정
            
            if(word.length()>1) {
            
                for(int j=1; j<word.length(); j++) {
                    
                    if(checkArray[word.charAt(j)-97== -1 && word.charAt(j) != word.charAt(j-1)) { // 한 번 나왔기도 했는데 앞의 알파벳과 다르면 그룹단어에서 탈락 
                        groupWordCount--;
                        break;
                    }
                    checkArray[word.charAt(j) - 97= -1// 해당 알파벳이 나오면 -1로 바꿈(나중에 체크할 시점에서 이미 한 번 나왔었다는 증거)
                }
            }
        }    
        bw.write(String.valueOf(groupWordCount));
        bw.flush();
    }
}
 
cs