컴퓨터 사이언스/1고리즘

백준 3300 : 무어 기계

저세상 개발자 2021. 8. 2. 23:42

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

 

3300번: 무어 기계

각 테스트 케이스마다, 지워진 심볼을 출력한다. 만약, 유일하게 결정할 수 없다면 '_'를 출력한다. 또, 입력으로 주어진 기계에서 만들 수 없는 경우에는 '!'를 출력한다.

www.acmicpc.net

 

정규표현식을 사용하면 아주 쉽게 풀 수 있는 문제고 그렇지 않다면 상당히 귀찮아지는 문제다.

문제의 입력으로 주어지는 무어 기계의 형식이 정규표현식과 동일하므로 정규표현식을 사용하여 문제를 해결하자.

 

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t, target_idx, ans_cnt; 
	string machine, s;
	char ans;

	cin >> t;
	while (t--) {
		cin >> machine >> s;
		target_idx = machine.find('_');
		ans_cnt = 0;

		for (char c = 'A'; c <= 'Z'; c++) {
			machine[target_idx] = c;
			regex re(machine);
			if (regex_match(s, re)) {
				ans = c;
				ans_cnt++;
			}
		}

		if (ans_cnt > 1) cout << '_' << '\n';
		else if (!ans_cnt) cout << '!' << '\n';
		else cout << ans << '\n';
	}

	return 0;
}
메모리: 2304 kb 시간: 80 ms