본문 바로가기

Algorithm_BOJ(백준)/문자열

[백준 1316 c++ OO] 그룹 단어 체커

728x90
반응형

- 풀이 링크:

https://github.com/xhaktmchl/Algorithm_study/blob/main/BOJ/%EB%AC%B8%EC%9E%90%EC%97%B4/%5B%EB%B0%B1%EC%A4%80%201316%20c%2B%2B%20OO%5D%20%EA%B7%B8%EB%A3%B9%20%EB%8B%A8%EC%96%B4%20%EC%B2%B4%EC%BB%A4.cpp

 

GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이

알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.

github.com

#include <iostream>
#include <algorithm>
//#include <map> // 중복 x 
#include <string> // getline
#include <cstring> // memset, strok, strstr
//#include <vector>
//#include <queue>
//#include <set> // 트리, 중복 x
//#include <unordered_set>
using namespace std;
/*
[백준 1316 c++ OO] 그룹 단어 체커
접근1: 
시간복잡도: n*n
풀이: 
    //1.입력
    //2.완탐: 문자열 탐색하며 전과 다른 문자가 나왔는데 이미 그 문자가 나온적 있으면 그룹단어 아님
    //3.출력:
*/
int n;
string a;
int cnt[26 + 10];
int res = 0;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
    
    //1.입력
    cin >> n;
    //2.완탐: 문자열 탐색하며 전과 다른 문자가 나왔는데 이미 그 문자가 나온적 있으면 그룹단어 아님
    while (n--) {
        bool isGroup = 1;
        memset(cnt, 0, sizeof(cnt));
        cin >> a;
        
        cnt[a[0] - 'a']++;
        int size = a.size();
        for (int i = 1; i < size; i++) {
            if (cnt[a[i] - 'a'] == 0) cnt[a[i] - 'a']++;
            else if (a[i] != a[i - 1] && cnt[a[i]-'a']!=0) {
                isGroup = 0;
                break;
            }
        }

        if (isGroup) res++;
    }
    //3.출력:
    cout << res << '\n';
    return 0;
}
반응형