이쿠의 슬기로운 개발생활

함께 성장하기 위한 보안 개발자 EverNote 내용 공유

코딩테스트

[프로그래머스][C++] 영어 끝말잇기

이쿠우우 2022. 4. 15. 19:03
반응형

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>
#include <algorithm>

using namespace std;

vector<int> solution(int n, vector<string> words) {
    vector<int> answer;
    
    vector<string> save;
    int count =0;
    char next;
    char now;
    for(string nowWord : words){        
        
        if(0 == count){
            next = nowWord[nowWord.size()-1];            
            count++;
            save.push_back(nowWord);
            continue;
        }
        
        
        auto it = find(save.begin(), save.end(), nowWord);
        if(it != save.end()){
            break;
        }    
        save.push_back(nowWord);
        
        now = nowWord[0];                
        if(next != now){                
            break;
        }       
        
        next = nowWord[nowWord.size()-1];
        count++;
    }
    
    if(words.size() == count){
        answer.push_back(0);
        answer.push_back(0);
    }
    else{
        answer.push_back((count%n)+1);
        answer.push_back((count/n)+1);
        
    }

    return answer;
}





꾸준히 하다보면 실력이 늘겠지..

반응형