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

 

코딩테스트 연습 - 영어 끝말잇기

3 ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"] [3,3] 5 ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"] [0,0]

programmers.co.kr

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

using namespace std;

vector<int> solution(int n, vector<string> words) {
    vector<int> answer;

    vector<vector<string>> vec(10);

    int a = 0;
    int charSize = words[a].size();
    char last_ch = words[a][charSize - 1];

    int round = 1;
    while (1) {

        for (int i = 0; i < n; i++) {

            if (a == 0) {
                vec[i].push_back(words[a]);
                a++;
                continue;
            }

            //주어진 단어들로 탈락자가 생기지 않는다면, [0, 0]을 return 해주세요.
            if (a == words.size()) {
                answer.push_back(0);
                answer.push_back(0);
                return answer;
            }

            //이미 나왔던 단어면 탈락
            for (int x = 0; x < a; x++) {
                if (words[a] == words[x]) {
                    answer.push_back(i+1);
                    answer.push_back(round);
                    return answer;
                }
            }

            if (last_ch == words[a][0]) {
                vec[i].push_back(words[a]);
                charSize = words[a].size();
                last_ch = words[a][charSize - 1];
            }

            //끝말잇기 실패 탈락자 발생
            else {
                answer.push_back(i+1);
                answer.push_back(round);
                return answer;
            }

            a++;
        }
        round++;
    }

}

+ Recent posts