[C++][정올][Intermediate_Coder] 1027 : 좋은수열

[C++][정올][Intermediate_Coder] 1027 : 좋은수열

코드

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int N;
char str[] = {'1','2','3'};
vector<string> answer;
 
void dfs(string ans){
    if (ans.length()==N){
        answer.push_back(ans);
        return;
    }
    if (!answer.empty()){
        return;
    }
    for (char c : str){
        string cp = ans+c;
        int n = cp.length();
        bool ck = true;
        for (int i=2; i<=n ; i+=2){
            if (cp.substr(n-i/2,i/2)==cp.substr(n-i,i/2)){
                ck=false;
                break;
            }
        }
        if (ck){
            dfs(cp);
        }
    }
}
 
int main() {
    cin>>N;
    dfs("");
    cout<<answer[0];
}

Discussion and feedback