본문 바로가기

프로그래머스 문제풀이/LEVEL 2

[C++] 프로그래머스 문제풀이 LEVEL 2 영어 끝말잇기

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/12981

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

#include <bits/stdc++.h>


std::vector<int> solution(int n, std::vector<std::string> words) {
    std::vector<int> answer;
    std::unordered_set<std::string> history;
    std::string preWord = words[0];
    history.insert(words[0]);
    std::string currWord;
    for (int i = 1; i < words.size(); ++i){
        currWord = words[i];
        if (history.find(currWord) != history.end() || preWord.back() != currWord[0]){
            return {i % n + 1, i / n + 1};
        }
        history.insert(currWord);
        preWord = currWord;
    }
    return {0, 0};
}