티스토리 뷰

https://www.acmicpc.net/problem/2661

 

2661번: 좋은수열

첫 번째 줄에 1, 2, 3으로만 이루어져 있는 길이가 N인 좋은 수열들 중에서 가장 작은 수를 나타내는 수열만 출력한다. 수열을 이루는 1, 2, 3들 사이에는 빈칸을 두지 않는다.

www.acmicpc.net

const fs = require('fs');
const N = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v=>+v)[0];
let end = false;

function solve(str){
  if(end) return;
  if(str.length==N){
    console.log(str)
    end = true;
    return;
  }else{
    for(let i = 1; i<=3; i++){
      const temp = str+`${i}`;
      if(temp.length<=N && isGood(temp)){
        solve(temp);
      }
    }
  }
}




function isGood(str){
  const L = str.length;
  const C = Math.floor(L/2)
  for(let i = 1; i<=C; i++){
    const A = L
    const B = L-i
    const C = L-i*2
    if(C>=0 && str.substring(B,A)==str.substring(C,B)){
      return false;
    }
  }
  return true;
}

solve('1')
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함