Skip to content

[권혁준-20주차 알고리즘 스터디] #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions 권혁준_20주차/[BOJ-21276] 계보 복원가 호석.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M;
static TreeMap<String, List<String>> V;
static TreeMap<String, List<String>> R;
static TreeMap<String, Integer> D;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception {

N = nextInt();
V = new TreeMap<>();
D = new TreeMap<>();
R = new TreeMap<>();
for(int i=1;i<=N;i++) {
String a = nextToken();
V.put(a, new ArrayList<>());
R.put(a, new ArrayList<>());
D.put(a, 0);
}

for(M = nextInt(); M-->0;){
String a = nextToken(), b = nextToken();
V.get(b).add(a);
D.put(a, D.get(a) + 1);
}

}

static void solve() throws Exception {

List<String> roots = new ArrayList<>();
TreeMap<String, Boolean> Z = new TreeMap<>();
for(String key : D.keySet()) Z.put(key, D.get(key) == 0);

for(String key : D.keySet()) if(Z.get(key)) {
roots.add(key);
Queue<String> Q = new LinkedList<>();
Q.offer(key);
while(!Q.isEmpty()) {
String n = Q.poll();
for(String i : V.get(n)) {
D.put(i, D.get(i) - 1);
if(D.get(i) == 0) {
R.get(n).add(i);
Q.offer(i);
}
}
}
}
bw.write(roots.size() + "\n");
for(String i:roots) bw.write(i + " ");
bw.write("\n");

for(String key:R.keySet()) {
bw.write(key + " " + R.get(key).size() + " ");
Collections.sort(R.get(key));
for(String i:R.get(key)) bw.write(i + " ");
bw.write("\n");
}


}

}
```
83 changes: 83 additions & 0 deletions 권혁준_20주차/[BOJ-2306] 유전자.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
```java
import java.util.*;
import java.io.*;

class IOController {
BufferedReader br;
BufferedWriter bw;
StringTokenizer st;

public IOController(){
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer("");
}

void nextLine() throws Exception {
st = new StringTokenizer(br.readLine());
}

String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}

int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
long nextLong() throws Exception { return Long.parseLong(nextToken()); }
double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }

void close() throws Exception {
bw.flush();
bw.close();
}

void write(String content) throws Exception {
bw.write(content);
}

}

public class Main {

static IOController io;

//

static char[] A;
static int[][] dp;
static int N;

public static void main(String[] args) throws Exception {

io = new IOController();

init();
solve();

io.close();

}

public static void init() throws Exception {

A = ("?" + io.nextToken()).toCharArray();
N = A.length-1;
dp = new int[N+1][N+1];

}

static void solve() throws Exception {

for(int k=1;k<N;k++) for(int i=1;i+k<=N;i++) {
for(int j=i;j<i+k;j++) dp[i][i+k] = Math.max(dp[i][i+k], dp[i][j] + dp[j+1][i+k]);
if((A[i] == 'a' && A[i+k] == 't') || (A[i] == 'g' && A[i+k] == 'c')) {
dp[i][i+k] = Math.max(dp[i][i+k], dp[i+1][i+k-1] + 2);
}
}
io.write(dp[1][N] + "\n");

}


}
```
76 changes: 76 additions & 0 deletions 권혁준_20주차/[BOJ-25381] ABBC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static List<Integer> A, B, C;
static boolean[] RB;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception {

A = new ArrayList<>();
B = new ArrayList<>();
C = new ArrayList<>();
char[] temp = nextToken().toCharArray();
for(int i=0;i<temp.length;i++) {
if(temp[i] == 'A') A.add(i);
if(temp[i] == 'B') B.add(i);
if(temp[i] == 'C') C.add(i);
}
RB = new boolean[B.size()];

}

static void solve() throws Exception {

int ans = 0;
for(int i=0, j=0;i<B.size() && j<C.size();i++) {
while(j < C.size() && B.get(i) > C.get(j)) j++;
if(j < C.size()) {
ans++;
RB[i] = true;
j++;
}
}

for(int i=0, j=0;i<A.size() && j<B.size();i++) {
while(j < B.size() && (RB[j] || A.get(i) > B.get(j))) j++;
if(j < B.size()) {
ans++;
j++;
}
}

bw.write(ans + "\n");

}

}
```
84 changes: 84 additions & 0 deletions 권혁준_20주차/[BOJ-2637] 장난감 조립.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M;
static int[][] A;
static List<int[]>[] V;
static int[] deg;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
A = new int[N+1][N+1];
V = new List[N+1];
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
deg = new int[N+1];
for(int i=0;i<M;i++){
int a = nextInt(), b = nextInt(), c = nextInt();
deg[a]++;
V[b].add(new int[]{a,c});
}

}

static void solve() throws Exception {

boolean[] isBasic = new boolean[N+1];
for(int i=1;i<=N;i++) if(deg[i] == 0) isBasic[i] = true;

Queue<Integer> Q = new LinkedList<>();
for(int i=1;i<=N;i++) if(isBasic[i]) {
for(int[] j:V[i]) {
int x = j[0], c = j[1];
if(isBasic[i]) A[x][i] = c;
if(--deg[x] == 0) Q.offer(x);
}
}

while(!Q.isEmpty()) {
int now = Q.poll();
for(int[] j:V[now]) {
int x = j[0], c = j[1];
for(int i=1;i<=N;i++) if(isBasic[i]) A[x][i] += A[now][i] * c;
if(--deg[x] == 0) Q.offer(x);
}
}

for(int i=1;i<=N;i++) if(A[N][i] != 0) {
bw.write(i + " " + A[N][i] + "\n");
}

}

}
```
Loading