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

 

1260번: DFS와 BFS

첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('./7568.txt').toString().trim().split("\n");
const [N, M ,V] = input.shift().split(" ").map( i => Number(i));
const graph = new Array(N+1).fill(0).map( i => []);

input.forEach((i) => {
   const [e0, e1] = i.split(" ").map( d => Number(d));

    graph[e0].push(e1);
    graph[e1].push(e0);
});



const dfs = (start) => {
    const visited = [];
    const stack = [start];

    while(stack.length) {
        const current = stack.pop();
        if(!visited.includes(current)) {
            visited.push(current);
            stack.push(...graph[current]);
        }
    }
    return visited;
}

const bfs = (start) => {
    const visited = [];
    const queue = [start];

    while(queue.length) {
        const current = queue.shift();
        if(!visited.includes(current)) {
            visited.push(current);
            queue.push(...graph[current]);
        }
    }
    return visited;
}

graph.forEach(v => v.sort((a, b) => b - a));
console.log(dfs(V).join(" "));
graph.forEach(v => v.sort((a, b) => a - b));
console.log(bfs(V).join(" "));
728x90

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

 

1049번: 기타줄

첫째 줄에 N과 M이 주어진다. N은 100보다 작거나 같은 자연수이고, M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에는 각 브랜드의 패키지 가격과 낱개의 가격이 공백으로 구분하여 주

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const [N, M] = input.shift().split(" ").map( i => Number(i));
const packages = [];
const singles = [];
let cost = 0;

input.forEach((i) => {
    const [p, s] = i.split(" ").map( m => Number(m));
    packages.push(p);
    singles.push(s);
});

let packagePay = Math.floor(N/6);
// packages 가격 중 최솟값이 singles 최솟값 * 6 보다 큰 경우 낱개로 사는게 이득
cost += Math.min(...packages) > Math.min(...singles) * 6 ? Math.min(...singles) * packagePay * 6 : Math.min(...packages) * packagePay;

// 낱개 최솟값 * 남은 구매 수 보다 패키지 최솟값으로 구매하는 것이 더 싼 경우 패키지 구매가 이득
cost += Math.min(...singles) * (N - (packagePay) * 6) < Math.min(...packages) ? Math.min(...singles) * (N - (packagePay) * 6) : Math.min(...packages)
console.log(cost);
728x90

문제 유형 : 문자열, 구현

난이도: 실버 3

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

 

2607번: 비슷한 단어

첫째 줄에는 단어의 개수가 주어지고 둘째 줄부터는 한 줄에 하나씩 단어가 주어진다. 모든 단어는 영문 알파벳 대문자로 이루어져 있다. 단어의 개수는 100개 이하이며, 각 단어의 길이는 10 이

www.acmicpc.net

const fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const N = Number(input.shift());
const STRING = input.shift();
const stringDic = {};
STRING.split("").forEach((s) => {
    if(stringDic[s] === undefined) {
        stringDic[s] = 1;
    } else {
        stringDic[s]++;
    }
});

const strArr = input;
let answer = 0;

// 문자가 구성을 갖는지 확인하는 함수
const sameWord = (str) => {
    let string = STRING.split("").sort().join("");
    str = str.split("").sort().join("");
    return string === str;
}

strArr.forEach((s) => {
    // 기준이 되는 단어보다 비교하는 단어의 길이가 1 작은 경우
    let string = STRING.split("").sort();
    let compare = s.split("").sort();

    for(let i = 0 ; i < string.length ; i++) {
        for(let j = 0 ; j < compare.length ; j++) {
            if(compare[j] === string[i]) {
                string[i] = '';
                compare[j] = '';
            }
        }
    }
    string = string.filter((s) => s !== '');
    compare = compare.filter((s) => s !== '');
   // 기준이 되는 단어와 비교하는 단어의 길이는 최대 1까지 차이 날 수 있다.
   if(s.length === STRING.length) {
       // 기준이 되는 단어와 비교하는 단어의 길이가 같은 경우
       if(sameWord(s)) {
           answer++;
       } else {
           if(string.length === 1 && compare.length === 1) {
               answer++;
           }
       }
   } else if(s.length +1 === STRING.length ) {
       if(string.length === 1 && compare.length === 0) {
           answer++;
       }
   } else if(s.length -1 === STRING.length ) {
       // 기준이 되는 단어보다 비교하는 단어의 길이가 1 큰 경우
       if(string.length === 0 && compare.length === 1) {
           answer++;
       }
   } else {
       // 그 외는 같은 구성의 단어가 될 수 없음.
   }
});

console.log(answer);
728x90

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

 

13305번: 주유소

표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 도시의 개수를 나타내는 정수 N(2 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 인접한 두 도시를 연결하는 도로의 길이가 제일 왼쪽 도로부터 N-1

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split("\n");

const N = input[0];
const km = input[1].split(" ").map( i => BigInt(i));
const cost = input[2].split(" ").map( i => BigInt(i));

let i = 0;
let current = cost[i];
let total = 0n;

while(true) {
    if(current >  cost[i]) {
        current =  cost[i];
    }
    total += current * km[i];
    i+=1;

    if(i === km.length) {
        break;
    }
}

console.log(String(total));

값이 크기 때문에 BigInt 를 쓰지 않으면 통과 하지 못함.

728x90

+ Recent posts