경험의 기록

문제 : www.acmicpc.net/problem/1316

 

1316번: 그룹 단어 체커

그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때

www.acmicpc.net

풀이

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
// 1316 그룹 단어 체커
#include <iostream>
using namespace std;
 
int checker(string s);
 
int main()
{
    int n;
    int count = 0;
 
    string s;
 
    cin >> n;
 
    for (int i = 0; i < n; i++) {
        cin >> s;
        count += checker(s);
    }
 
    cout << count;
    return 0;
}
 
int checker(string s) {
    int arr[26= { 0 }; // 각 알파벳 자리 0 초기화
 
    for (int i = 0; i < s.length(); i++) {
        if (arr[s[i] - 'a'== 1) {
            if (s[i - 1!= s[i]) {
                return 0;
            }
        } // 처음 등장한 알파벳이 아닐 때 그 전 글자가 동일한 글자가 아닐 경우 그룹 단어가 아니므로 0 리턴
        arr[s[i] - 'a'= 1// 처음으로 등장한 알파벳 1로
    }
 
    return 1// 그룹 단어일 경우 1 리턴
}
cs
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading