Skip to content
Merged
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
30 changes: 30 additions & 0 deletions 남지현/bruteForce/AAndB212919.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.*;
import java.io.*;

// 백준 12919 - A와 B 2 (골드5)

class Main {

static String S, T;

static boolean solve(String str) {
if (str.length() == S.length()) {
return S.equals(str.toString());
}
if (str.charAt(str.length()-1)=='A') {
if (solve(str.substring(0, str.length()-1))) return true;
}
if (str.charAt(0) == 'B') {
StringBuilder sb = new StringBuilder(str);
if (solve(sb.deleteCharAt(0).reverse().toString())) return true;
}
return false;
}

public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
S = bf.readLine();
T = bf.readLine();
System.out.println(solve(T)? 1: 0);
}
}
34 changes: 34 additions & 0 deletions 남지현/dp/CypherCode2011.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;
import java.io.*;

// 백준 2011 - 암호코드 (골드5)

class Main {

static final int MOD = 1_000_000;

static boolean isPositiveDigit(String str, int idx) {
return str.charAt(idx)>'0' && str.charAt(idx)<='9';
}

static boolean canDecode(String str, int lastIdx) {
return str.charAt(lastIdx-1)=='1' || str.charAt(lastIdx-1)=='2' && str.charAt(lastIdx)<='6';
}

public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String input = bf.readLine();
int[] counts = new int[input.length()+1];
counts[0]=1;
for (int i=1; i<counts.length; i++) {
if (isPositiveDigit(input, i-1)) {
counts[i] += counts[i-1];
}
if (i>=2 && canDecode(input, i-1)) {
counts[i] += counts[i-2];
}
counts[i]%=MOD;
}
System.out.println(counts[input.length()]);
}
}
60 changes: 60 additions & 0 deletions 남지현/graph/ACMCraft1005.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.*;
import java.io.*;

// 백준 1005 - ACM Craft (골드3) 위상 정렬

class Main {

static List<List<Integer>> graph;
static int[] costs, times, inDegree;
static int N;

static int solution(int destination) {
ArrayDeque<Integer> queue = new ArrayDeque<>();
for (int i=1; i<=N; i++) {
if (inDegree[i]==0) {
queue.addLast(i);
times[i] = costs[i];
}
}
while (! queue.isEmpty()) {
int now = queue.pollFirst();
for (int next: graph.get(now)) {
inDegree[next]--;
if (inDegree[next]==0) queue.addLast(next);
times[next] = Math.max(times[next], times[now]+costs[next]);
}
}
return times[destination];
}

public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
StringBuilder sb = new StringBuilder();
for (int t=0; t<T; t++) {
StringTokenizer st = new StringTokenizer(bf.readLine());
N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
costs = new int[N+1];
times = new int[N+1];
inDegree = new int[N+1];
graph = new ArrayList<>();
st = new StringTokenizer(bf.readLine());
graph.add(new ArrayList<>());
for (int i=1; i<=N; i++) {
costs[i] = Integer.parseInt(st.nextToken());
graph.add(new ArrayList<>());
}
for (int i=0; i<K; i++) {
st = new StringTokenizer(bf.readLine());
int dep = Integer.parseInt(st.nextToken());
int dst = Integer.parseInt(st.nextToken());
graph.get(dep).add(dst);
inDegree[dst]++;
}
sb.append(solution(Integer.parseInt(bf.readLine()))).append("\n");
}
System.out.print(sb);
}
}
92 changes: 92 additions & 0 deletions 남지현/graph/TheShortestPath1504.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.util.*;
import java.io.*;

// 백준 1504 - 특정한 최단 경로 (골드4)

class Main {

static Map<Integer, List<int[]>> graph;
static int[] dist;
static int N;

static final int MAX = 200_000_001;

static class Edge implements Comparable<Edge>{
int dest;
int value;

Edge(int dest, int value) {
this.dest = dest;
this.value = value;
}

public int compareTo(Edge e) {
return this.value-e.value;
}
}

private static void dijkstra(int departure) {
boolean[] visited = new boolean[N+1];
dist = new int[N+1];
Arrays.fill(dist, MAX);
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(departure, 0));
dist[departure]=0;
while (!pq.isEmpty()) {
Edge now = pq.poll();
if (visited[now.dest]) continue;
visited[now.dest] = true;
for (int[] next: graph.get(now.dest)) {
if (dist[next[0]] > dist[now.dest]+next[1]) {
dist[next[0]] = dist[now.dest]+next[1];
pq.add(new Edge(next[0], dist[next[0]]));
}
}
}
}

public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
N = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
graph = new HashMap<>();
for (int dep=1; dep<=N; dep++) {
graph.put(dep, new ArrayList<>());
}
for (int i=0; i<E; i++) {
st = new StringTokenizer(bf.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
graph.get(x).add(new int[]{y, v});
graph.get(y).add(new int[]{x, v});
}
st = new StringTokenizer(bf.readLine());
int v1 = Integer.parseInt(st.nextToken());
int v2 = Integer.parseInt(st.nextToken());

dijkstra(v1);
int v1ToV2 = dist[v2];

dijkstra(1);
int dstToV1 = dist[v1];
int dstToV2 = dist[v2];

dijkstra(N);
int v1ToN = dist[v1];
int v2ToN = dist[v2];

int answer = MAX * 3;
if (v1ToV2 != MAX && dstToV1 != MAX && v2ToN != MAX) {
answer = Math.min(answer, dstToV1 + v1ToV2 + v2ToN);
}
if (v1ToV2 != MAX && dstToV2 != MAX && v1ToN != MAX) {
answer = Math.min(answer, dstToV2 + v1ToV2 + v1ToN);
}
if (answer == MAX*3) {
answer = -1;
}
System.out.println(answer);
}
}
66 changes: 66 additions & 0 deletions 남지현/tree/TreeOrderGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.*;

// 프로그래머스 - 길 찾기 게임 (Lv 3)

class Solution {
int[][] answer;
int idx;

public int[][] solution(int[][] nodeinfo) {
answer = new int[2][nodeinfo.length];
List<Node> nodes = new ArrayList<>();
for (int i=0; i<nodeinfo.length; i++) {
nodes.add(new Node(i+1, nodeinfo[i][0], nodeinfo[i][1]));
}
Collections.sort(nodes);
Node root = nodes.get(0);
for (int i=1; i<nodes.size(); i++) {
makeTree(root, nodes.get(i));
}
idx=0;
preOrder(root);
idx=0;
postOrder(root);
return answer;
}

private void makeTree(Node parent, Node child) {
if (parent.x > child.x) {
if (parent.left == null) parent.left = child;
else makeTree(parent.left, child);
} else {
if (parent.right == null) parent.right = child;
else makeTree(parent.right, child);
}
}

private void preOrder(Node node) {
answer[0][idx++] = node.num;
if (node.left != null) preOrder(node.left);
if (node.right != null) preOrder(node.right);
}

private void postOrder(Node node) {
if (node.left != null) postOrder(node.left);
if (node.right != null) postOrder(node.right);
answer[1][idx++] = node.num;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열에 idx를 밖으로 빼고 바로 넣어주셨네요 !!

}

static class Node implements Comparable<Node> {
int num;
int x;
int y;
Node left;
Node right;

Node(int num, int x, int y) {
this.num = num;
this.x = x;
this.y = y;
}

public int compareTo(Node node) {
return this.y==node.y? this.x-node.x: node.y-this.y;
}
}
}