※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 24820번 문제인 Spelling Bee이다.
문제는 아래 링크를 확인하자.

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

 

24820번: Spelling Bee

The input consists of a single test case, which starts with a line with $7$ distinct lowercase English letters. The first of these letters is the center letter.  The next line contains an integer $n$ ($1 \le n \le 102\,305$), the size of the dictionary. T

www.acmicpc.net

조건을 만족하는 문자열들을 찾아내는 유형의 문제이다.

 

구체적으로는 맨 처음에 주어지는 문자열을 X, 나중에 주어지는 문자열들을 각 Y라고 하였을 때, X를 이루는 알파벳만으로 이루어져있고 X[0]을 포함하면서 길이가 4 이상인 Y들을 입력받은 순서대로 출력하는 문제이다.

 

X를 구성하고 있는 문자열을 배열을 이용하여 쉽게 접근하게 할 수 있다.

 

아래는 제출한 소스코드이다.

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

char centerletter;
bool letters[128];

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

	string s; cin >> s;
	centerletter = s[0];
	for (auto l : s) letters[l] = 1;

	int T; cin >> T;
	while (T--) {
		cin >> s;
		bool letterchk = 1;
		bool centerchk = 0;
		for (auto l : s) {
			if (l == centerletter) centerchk = 1;
			if (!letters[l]) letterchk = 0;
		}
		if (letterchk && centerchk && (int) s.length() > 3) cout << s << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 24818 // C++] Field Trip  (0) 2022.03.27
[BOJ 24822 // C++] Musical Trees  (0) 2022.03.26
[BOJ 24751 // C++] Betting  (0) 2022.03.24
[BOJ 24745 // C++] Morse Code Palindromes  (0) 2022.03.23
[BOJ 9933 // C++] 민균이의 비밀번호  (0) 2022.03.22

+ Recent posts