[C++]백준1316번 그룹 단어 체커

less than 1 minute read

오늘은 백준1316번 그룹 단어 체커 문제를 풀어보도록 하자.

아스키코드 변환을 통해서 알파벳 a부터 z까지 변환 후 boolean 배열에 체크해주는 방식으로 했다.

문제는 다음과 같다.

                               [접근법]

  1. alphabet - 'a'로 아스키코드 변환 후 배열에서 이미 나왔던 단어인지, 연속된 단어인지 체크를 해주었다. 쉬운 문제라서 딱히 쓸게 없당..
                                     [코드]
#include <iostream>
#include <string>
using namespace std;

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

	string word;
	int N;
	int result = 0;

	cin >> N;

	for (int i = 0; i < N; i++) {
		bool flag = false;
		bool check[26] = { false, };
		char alphabet;

		cin >> word;

		alphabet = word[0];
		check[alphabet - 'a'] = true;

		for (int j = 1; j < word.length(); j++) {
			if (alphabet != word[j]) {
				if (check[word[j] - 'a']) {
					flag = true;
					break;
				}
				else {
					check[word[j] - 'a'] = true;
					alphabet = word[j];
				}
			}
		}
		if (!flag) {
			result++;
		}
	}
	cout << result << '\n';
	return 0;
}