안녕 세상아,

[백준/c++] 1316 그룹 단어 체커 본문

백준

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

돈 많은 백수가 되고싶다 2024. 12. 17. 21:25

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

 

그룹 단어 구별하는 방법

1. 일단 각 알파벳 별 배열로 만들고 false로 세팅하기

2. 처음 나오는 알파벳은 일단 true로 바꾸기

3. 앞뒤 문자 비교해서 같으면 넘어가기 (continue)

4. 앞뒤 문자 다르고 해당 알파벳이 true라면 cnt++하기

5. cnt는 그룹 단어가 아니기 때문에 n에서 cnt 빼줌 

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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n;
	cin >> n;

	int cnt = 0;

	for (int i = 0; i < n; i++) {
		string str;
		cin >> str;
		bool arr[26] = { false, };

		arr[str[0] - 'a'] = true;
		for (int j = 1; j < str.size(); j++) {
			if (str[j] == str[j - 1])
				continue;
			else if (str[j] != str[j - 1] && arr[str[j] - 'a'] == true) {
				cnt++;
				break;
			}
			else
				arr[str[j] - 'a'] = true;
		}
	}
	cout << n - cnt;
}