1316번 - 그룹 단어 체커 #구현

esc247·2022년 7월 5일
0

Algorithm

목록 보기
4/11

1316번

주어진 문자열에서 중복된 문자가 연속으로 된 경우가 2번 이상이면 그룹단어가 아니다.

string의 erase,unique를 활용하면 쉽게 풀린다.
unique로 연속된 중복원소를 뒤로 보내고 erase로 쓰레기 값을 제거하면 된다.

#include <string>
string str;
str.erase(
unique(	//중복값을 맨 뒤로 보내고, 그 중복값이 시작하는 iterator 반환
str.begin(), str.end() ), str.end());

전체 코드

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int n, cnt = 0;
    cin >> n;
    while (n--)
    {
        string str;
        cin >> str;
        bool tag = true;
        str.erase(unique(str.begin(), str.end()), str.end());
        sort(str.begin(), str.end());
        for (int i = 0; i < str.length(); i++)
        {
            if (str[i] == str[i + 1])
            {
                tag = false;
                break;
            }
        }
        if (tag)
            cnt++;
    }
    cout << cnt << '\n';
    return 0;
}
profile
막상 하면 모르니까 일단 하자.

0개의 댓글