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

 

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

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

 

24793번: Shiritori

If the game was played according to the rules, output "Fair Game". Otherwise, find out which player first violated the rules of the game.  That player lost the game, so output "Player lost ". For example, if player $1$ violated the rules first, output "Pl

www.acmicpc.net

문제에서 주어지는 게임의 룰에 따르면, 이 문제에서는 같은 단어를 중복으로 처음 사용하거나 앞선 단어의 끝의 철자로 시작하지 않은 단어를 처음 말한 사람이 진다.

 

사용한 단어를 set을 이용하여 관리하면 문제를 간단히 구현할 수 있다.

 

string s에서 첫 글자는 s[0], 마지막 글자는 s.back()으로 쉽게 접근 가능하다.

 

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

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

set<string> st;

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

	string s;
	int N; cin >> N >> s;
	char l = s.back();
	st.insert(s);
	
	for (int i = 1; i < N; i++) {
		cin >> s;
		if (st.find(s) != st.end() || s[0] != l) {
			if (i & 1) cout << "Player 2 lost";
			else cout << "Player 1 lost";
			return 0;
		}
		l = s.back();
		st.insert(s);
	}

	cout << "Fair Game";
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 24930 // C++] Ordinary Ordinals  (0) 2022.04.17
[BOJ 23854 // C++] The Battle of Giants  (0) 2022.04.17
[BOJ 24356 // C++] ЧАСОВНИК  (0) 2022.04.17
[BOJ 3002 // C++] 아날로그 다이얼  (0) 2022.04.16
[BOJ 3110 // C++] 부등식  (0) 2022.04.15

+ Recent posts