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

 

이번에 볼 문제는 백준 24978번 문제인 Subset Equality이다.
문제는 아래 링크를 확인하자.

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

 

24978번: Subset Equality

The cows are trying out a new method of exchanging coded messages with each-other where they mix irrelevant letters in among relevant letters to make the messages hard to decode. The cows transmit two strings $s$ and $t$ each of length at most $10^5$ consi

www.acmicpc.net

주어진 쿼리를 구성하는 문자의 개수가 둘 이상일 때, 어떤 두 문자를 뽑아도 그 두 문자만으로 제한된 문자열이 같다면 주어진 쿼리에 있는 모든 문자로 제한된 문자열 또한 같다는 성질을 발견하는 것으로 문제를 해결할 수 있다. 위 성질을 발견하기만 한다면, 증명은 어렵지 않다.

 

문자의 개수가 하나인 경우 예외처리를 하는 것을 잊지 말자.

 

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

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

string s1, s2;
bool chk[100000];
int cnt[128];

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

	cin >> s1 >> s2;
	for (auto l : s1) cnt[l]++;
	for (auto l : s2) cnt[l]--;

	for (char c = 'a'; c < 's'; c++) {
		for (char d = c + 1; d < 's'; d++) {
			string ss1 = "", ss2 = "";
			for (auto l : s1) {
				if (l == c || l == d) ss1 += l;
			}
			for (auto l : s2) {
				if (l == c || l == d) ss2 += l;
			}
			if (ss1 == ss2) chk[((int)c) * 128 + d] = 1;
		}
	}

	int Q; cin >> Q;
	while (Q--) {
		bool ans = 1;
		string ss; cin >> ss;
		int sslen = ss.length();
		if (sslen == 1) {
			if (cnt[ss[0]]) cout << 'N';
			else cout << 'Y';
		}
		else {
			for (int i = 0; i < sslen; i++) {
				for (int j = i + 1; j < sslen; j++) {
					if (!chk[((int)ss[i]) * 128 + ss[j]]) ans = 0;
				}
			}

			if (ans) cout << 'Y';
			else cout << 'N';
		}
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 24923 // C++] Canadians, eh?  (0) 2022.05.01
[BOJ 24977 // C++] Visits  (0) 2022.04.30
[BOJ 24979 // C++] COW Operations  (0) 2022.04.28
[BOJ 24980 // C++] Photoshoot  (0) 2022.04.27
[BOJ 24981 // C++] Counting Liars  (0) 2022.04.26

+ Recent posts