Development./Problem solving.

[BAEKJOON] 1316 - 그룹 단어 체커

Chuuu_DevCamp:) 2020. 8. 17. 19:27
반응형

https://www.acmicpc.net/problem/1316

한 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우 그룹 단어라고 한다.
입력 받은 스트링에 각 문자가 유효한 지 체크하기 위해 이전에 체크한 문자와 현재 인덱스의 문자를 비교하고, 틀릴 경우 checker string에 해당 문자를 삽입한다.
만약 이전 문자와 현재 문자가 틀린데 checker string에 해당 문자가 있다면, 체크 중인 단어는 그룹 단어가 아니므로 count에서 제외 한다.

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    std::ios::sync_with_stdio(false);
    int testCase = 0, result = 0;
    std::cin >> testCase;
    while(testCase--)
    {
        std::string inp;
        std::cin >> inp;

        int inpSize = static_cast<int>(inp.size());
        char prevChar = inp[0];
        std::string checker;
        checker.push_back(prevChar);
        result++;
        for(int idx = 1; idx < inpSize; idx++)
        {
            if (prevChar != inp[idx])
            {
                prevChar = inp[idx];
                if(std::string::npos != checker.find(inp[idx]))
                {
                    result--;
                    break;
                }
                else
                {
                    checker.push_back(inp[idx]);
                }
                
            }
        }
    }

    std::cout << result;
    return 0;
}

'Development. > Problem solving.' 카테고리의 다른 글

[BAEKJOON] 2745 - 진법 변환  (0) 2020.08.17
[BAEKJOON] 2468 - 안전 영역  (0) 2020.08.17
[BAEKJOON] 1157 - 단어 공부  (0) 2020.08.17
[BAEKJOON] 2675 - 문자열 반복  (0) 2020.08.17
[BAEKJOON] 4963 - 섬의 개수  (0) 2020.08.17