diff --git a/code_study/Baekjoon/java/_9251.java b/code_study/Baekjoon/java/_9251.java new file mode 100644 index 0000000..c9acc59 --- /dev/null +++ b/code_study/Baekjoon/java/_9251.java @@ -0,0 +1,25 @@ +import java.util.*; + +public class _9251 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String s1 = sc.nextLine().trim(); + String s2 = sc.nextLine().trim(); + sc.close(); + + int l1 = s1.length(), l2 = s2.length(); + int[][] lcs = new int[l1+1][l2+1]; + + for(int i=1; i<=l1; i++) { + for(int j=1; j<=l2; j++) { + if(s1.charAt(i-1) == s2.charAt(j-1)) { + lcs[i][j] = lcs[i-1][j-1] + 1; + } + else { + lcs[i][j] = Math.max(lcs[i-1][j], lcs[i][j-1]); + } + } + } + System.out.println(lcs[l1][l2]); + } +} diff --git a/code_study/Baekjoon/swift/9251.swift b/code_study/Baekjoon/swift/9251.swift new file mode 100644 index 0000000..4f2b501 --- /dev/null +++ b/code_study/Baekjoon/swift/9251.swift @@ -0,0 +1,21 @@ +if let s1 = readLine(), let s2 = readLine() { + let arr1: [Character] = Array(s1) + let arr2: [Character] = Array(s2) + let l1: Int = arr1.count + let l2: Int = arr2.count + var dp: [[Int]] = Array(repeating: Array(repeating:0, count: l2+1), count : l1+1) + + for i in 1...l1 { + let c1: Character = arr1[i-1] + for j in 1...l2 { + let c2: Character = arr2[j-1] + if c1==c2 { + dp[i][j] = dp[i-1][j-1]+1 + } + else { + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + } + } + } + print(dp[l1][l2]) +} \ No newline at end of file