-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from HYU-PS-STUDY/clean2001
[week-05-dp] 25215, 2533
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 백준 25215. 타이핑 | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class B25215 { | ||
static String s; | ||
static int dp[][]; | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
s = br.readLine().trim(); | ||
dp = new int[2][s.length()+1]; | ||
|
||
s = " " + s; // 인덱스를 맞춰주기 위해 (문자열의 시작을 1부터) | ||
|
||
dp[0][0] = 0; // 0행 -> 마름모 비활성화 | ||
dp[1][0] = 1; // 1행 -> 마름모 활성화 | ||
|
||
for(int i=1; i<s.length(); ++i) { | ||
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z') { // 소문자이면 | ||
dp[0][i] = Math.min(dp[0][i-1] + 1, dp[1][i-1]+2); | ||
dp[1][i] = dp[1][i-1] + 2; // 활성화를 유지 | ||
} else { // 대문자이면 | ||
dp[1][i] = Math.min(dp[1][i-1] + 1, dp[0][i-1]+2); | ||
dp[0][i] = dp[0][i-1] + 2; // 비활유지 | ||
} | ||
} | ||
|
||
int ans = Math.min(dp[0][s.length()-1], dp[1][s.length()-1]); | ||
|
||
System.out.println(ans); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 2533. 사회망서비스(SNS) | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class B2533 { | ||
static int N; | ||
static List<List<Integer>> adj; | ||
static int[][] dp; // 행: 현재 노드가 얼리어답터인지 아닌지, 열: 현재 노드 번호, 값: 현재 노드부터 시작해서 리프까지 얼리어답터 최소 수 | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
N = Integer.parseInt(br.readLine()); | ||
adj = new ArrayList<>(); | ||
dp = new int[2][N+1]; | ||
|
||
for(int i=0; i<=N; ++i) { | ||
adj.add(new ArrayList<>()); | ||
} | ||
|
||
for(int i=0; i<N-1; ++i) { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int a = Integer.parseInt(st.nextToken()); | ||
int b = Integer.parseInt(st.nextToken()); | ||
|
||
adj.get(a).add(b); | ||
adj.get(b).add(a); | ||
} | ||
|
||
dfs(1, 0); | ||
System.out.println(Math.min(dp[0][1], dp[1][1])); | ||
} | ||
|
||
static void dfs(int cur, int prev) { | ||
dp[0][cur] = 0; | ||
dp[1][cur] = 1; | ||
|
||
List<Integer> nexts = adj.get(cur); | ||
for(int next : nexts) { | ||
if(next == prev) continue; | ||
|
||
dfs(next, cur); | ||
dp[0][cur] += dp[1][next]; // cur가 얼리어가 아니므로, next가 무조건 얼리어야만 함 | ||
dp[1][cur] += Math.min(dp[1][next], dp[0][next]); // cur가 얼리어이므로, next는 뭐든 상관없음 | ||
} | ||
} | ||
} |