모각코

[2021 동계 모각코] 6회차 회고록

narlo 2022. 2. 17. 00:31

6회차(2022.02.08 15:00~18:00)

 

오늘의 목표

백준 3문제 풀기

 

1) 14888 연산자 끼워넣기

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

 

14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 

www.acmicpc.net

import java.util.*;
import java.io.*;

public class Main {
    static int n;
    static int[] num;
    static char[] operator = {'+', '-', '*', '/'};
    static long max = Long.MIN_VALUE;
    static long min = Long.MAX_VALUE;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        n = Integer.parseInt(input);
        num = new int[n];

        input = br.readLine();
        String[] s = input.split(" ");
        for(int i=0;i<n;i++) {
            num[i] = Integer.parseInt(s[i]);
        }

        input = br.readLine();
        String[] ss = input.split(" ");
        int[] op = new int[4];
        for(int i=0;i<4;i++) {
            op[i] = Integer.parseInt(ss[i]);
        }

        dfs(op, num[0], 1);
        System.out.println(max);
        System.out.println(min);
    }

    static void dfs(int[] op, long result, int start) {
        if(start>=n) {
            if(result>max) {
                max=result;
            }
            if(result<min) {
                min=result;
            }
            return;
        }
        for(int i=0;i<4;i++) {
            if(op[i]>0) {
                op[i]--;
                dfs(op, calc(operator[i], result, num[start]), start +1);
                op[i]++;
            }
        }
    }

    static long calc(char op, long result, int target) {
        if(op=='+') {
            result += target;
        } else if(op=='-') {
            result -= target;
        } else if(op=='*') {
            result *= target;
        } else {
            result = (long)(result/target);
        }
        return result;
    }
}

 

2) 10819 차이를 최대로

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

 

10819번: 차이를 최대로

첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.

www.acmicpc.net

import java.util.*;
import java.io.*;

public class Main {
    static int n;
    static int[][] cha;
    static boolean[] visit;
    static int max = Integer.MIN_VALUE;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        n = Integer.parseInt(input);
        int[] num = new int[n];

        input = br.readLine();
        String[] s = input.split(" ");
        for(int i=0;i<n;i++) {
            num[i] = Integer.parseInt(s[i]);
        }

        cha = new int[n][n];
        visit = new boolean[n];

        for(int i=0;i<n;i++) {
            for(int j=i;j<n;j++) {
                int tmp = Math.abs(num[i]-num[j]);
                cha[i][j] = tmp;
                cha[j][i] = tmp;
            }
        }

        dfs(0, 0, 0);
        System.out.println(max);
    }

    static void dfs(int i, int result, int cnt) {
        if(cnt==n) {
            max = Math.max(max, result);
            return;
        }
        for(int j=0;j<n;j++) {
            if(!visit[j]) {
                visit[j]=true;
                dfs(j, cnt==0 ? 0 : result+cha[i][j], cnt+1);
                visit[j]=false;
            }
        }
    }
}

 

3) 1759 암호 만들기

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

 

1759번: 암호 만들기

첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.

www.acmicpc.net

import java.util.*;
import java.io.*;

public class Main {
    static int l;
    static int c;
    static char[] password;
    static char[] vowel = {'a', 'e', 'i', 'o', 'u'};
    static boolean[] visit;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        String[] s = input.split(" ");
        l = Integer.parseInt(s[0]);
        c = Integer.parseInt(s[1]);

        password = new char[c];
        visit = new boolean[c];
        input = br.readLine();
        String[] ss = input.split(" ");
        for(int i=0;i<c;i++) {
            password[i] = ss[i].charAt(0);
        }

        Arrays.sort(password);

        dfs(0, 0, "");
    }

    static void dfs(int i, int cnt, String result) {
        if(cnt==l) {
            int vowel_cnt = isVowel(result);
            if(vowel_cnt>=1 && l-vowel_cnt>=2) {
                System.out.println(result);
            }
            return;
        }
        for(int j=i; j<c;j++) {
            if(!visit[j]) {
                visit[j]=true;
                dfs(j, cnt+1, result + password[j]);
                visit[j]=false;
            }
        }
    }

    static int  isVowel(String result) {
        int cnt=0;
        for(int i=0;i<l;i++) {
            char tmp = result.charAt(i);
            for(char alpha : vowel) {
                if(tmp==alpha) {
                    cnt++;
                }
            }
        }
        return cnt;
    }
}

 

4) 14425 문자열 집합

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

 

14425번: 문자열 집합

첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다.  다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어

www.acmicpc.net

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        String[] s = input.split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);

        Map<String, Integer> map = new TreeMap<>();
        for(int i=0;i<n;i++) {
            input = br.readLine();
            map.put(input, 1);
        }

        int cnt=0;
        for(int j=0;j<m;j++) {
            input = br.readLine();
            if(map.get(input)!=null) {
                cnt++;
            }
        }

        System.out.println(cnt);
    }

}

학기 시작 전 알고리즘을 복습하는 시간을 가졌다.

항상 시작이 어려웠는데, 모각코를 진행하며 알고리즘 복습을 시작했고,

모각코 활동이 끝나더라도 꾸준히 해서 학기 시작 전에 완료해야겠다.