그럴듯한 개발 블로그
반응형

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

 

프로그래머스

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

programmers.co.kr

unorderedset 사용해 줬다. 나오지 않은 단어일 때, 끝말잇기 될 때 조건 처리 했다.

#include <string>
#include <vector>
#include <iostream>
#include <unordered_set>

using namespace std;

vector<int> solution(int n, vector<string> words) {
    vector<int> answer;
    char    first_char = words[0][0];
    unordered_set<string>  s; // 여태 사용한 단어 담기
    
    for (int i = 0; i < words.size(); i++)
    {
        if (s.find(words[i]) == s.end() && first_char == words[i][0]) // 여태 나오지 않은 단어면 , 끝말잇기 성립되면
        {
            s.insert(words[i]);
            first_char = words[i][words[i].size() - 1];
        }
        else
        {
            answer.push_back(i % n + 1);
            answer.push_back(i / n + 1);
            return (answer);
        }
    }
    answer.push_back(0);
    answer.push_back(0);
    return answer;
}
반응형
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!